运行Capybara测试时未定义的方法“ map”

时间:2019-09-17 12:41:15

标签: ruby-on-rails ruby-on-rails-5 capybara

我正在使用Capybara进行功能测试,但始终收到此错误:

 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'

测试是:

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

视图中的代码为:

<%= 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" %>

使用撬动,我可以检查User.is_architect列表是否存在。尝试执行select命令将引发以下情况:

[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'

在手动测试时,一切正常。我在这里想念什么?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,结果发现我包括ActionView::TestCase::Behavior,这会干扰事物。我可以通过在page语句(即select)中添加page.select @architect2.name, from: 'architect'限定词来解决此问题。

事实证明,对于此特定测试,我什至不需要ActionView::TestCase::Behavior,一旦删除它,我就可以删除page.,一切都很好。

因此,值得回顾一下所包含的模块,并添加page.限定符以确保其他内容不会妨碍您。

答案 1 :(得分:0)

您是否尝试清理资产 如果您在前端做了一些更改,这将很有帮助 它将清除您的资产并从头开始对其进行编译。 这可能有帮助 耙资产:清洁
耙资产:口语
耙资产:预编译