Search code examples
ruby-on-railsruby-on-rails-5capybara

Undefined method `map' while running Capybara test


I'm running a feature test using Capybara, but keep getting this error:

 Failure/Error: select(@architect2.name, from: 'architect')

 NoMethodError:
   undefined method `map' for nil:NilClass
   Did you mean?  tap
 # ./spec/features/reassign_team_spec.rb:30:in `block (2 levels) in <top (required)>'
 # ./spec/support/database_cleaner_spec.rb:58:in `block (2 levels) in <top (required)>'
 # ./spec/support/database_cleaner_spec.rb:39:in `block (2 levels) in <top (required)>'
 # /home/user/.rvm/gems/ruby-2.3.3/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:123:in `block in run'
 # /home/user/.rvm/gems/ruby-2.3.3/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:110:in `loop'
 # /home/user/.rvm/gems/ruby-2.3.3/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:110:in `run'
 # /home/user/.rvm/gems/ruby-2.3.3/gems/rspec-retry-0.6.1/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
 # /home/user/.rvm/gems/ruby-2.3.3/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:37:in `block (2 levels) in setup'

The test is:

click_button('Switch')
select(@architect2.name, from: 'architect')
select(@supervisor2.name, from: 'supervisor')

And the code in the view is:

<%= select_tag :architect, options_for_select(User.is_architect.collect{ |u| [u.name, u.id] }, params[:architect]), {:prompt => 'All', :class => "form-control", :required => true } %>
<%= select_tag :supervisor, options_for_select(User.is_supervisor.collect{ |u| [u.name, u.id] }, params[:supervisor]), prompt: 'All', class: "form-control" %>

Using pry I could check that the User.is_architect list exists. Trying to do the select command throws this:

[1] pry(#<RSpec::ExampleGroups::ReassignTeams>)> select(@architect2.name,from: 'architect')
NoMethodError: undefined method `map' for nil:NilClass
Did you mean?  tap
from /home/user/.rvm/gems/ruby-2.3.3/gems/actionview-5.2.3/lib/action_view/helpers/form_options_helper.rb:364:in `options_for_select'

Everything works just fine when testing manually. What am I missing here?


Solution

  • I had this same problem and it turned out that I was including ActionView::TestCase::Behavior which was interfering with things. I was able to work around this by adding a page qualifier to the select statement, i.e. page.select @architect2.name, from: 'architect'.

    It turns out I didn't even need ActionView::TestCase::Behavior for this particular test, once I removed it I could remove page. and all was well.

    So, it may be worth reviewing included modules, and adding the page. qualifier to make sure something else isn't getting in the way.