未定义的方法错误,但仅在测试中
Undefined method error but only in test
下面的测试生成错误消息
ActionView::Template::Error: undefined method `avatar?' for nil:NilClass
app/views/members/index.html.erb:25:in `block in _app_views_members_index_html_erb___1876261578458959373_9315040'
app/views/members/index.html.erb:22:in `_app_views_members_index_html_erb___1876261578458959373_9315040'
test/integration/site_layout_test.rb:50:in `block in <class:SiteLayoutTest>'
但在开发中它似乎有效。头像显示是否存在。有人知道原因吗?
测试:
get users_path
assert_template 'users/index'
User.paginate(page: 1).each do |user|
assert_select 'a[href=?]', user_path(user)
get organizations_path
assert_template 'organizations/index'
Organization.paginate(page: 1).each do |organization|
assert_select 'a[href=?]', organization_path(organization)
end
get members_path # THIS IS LINE 50!!
assert_template 'members/index'
Member.paginate(page: 1).each do |member|
assert_select 'a[href=?]', member_path(member)
end
会员索引视图包括:
<% @members.each do |member| %> # THIS IS LINE 22!!
<tr>
<td>
<% if member.organization.avatar? %> # THIS IS LINE 25!!
<%= link_to image_tag(member.organization.avatar.url, alt: "Profile"), member_path(member) %> <%= member.username %>
<% else %>
<%= link_to image_tag("profile.gif", alt: "Profile"), member_path(member) %> <%= member.username %>
<% end %>
</td>
<td><%= member.fullname %></td>
etc...
此错误表示您已尝试在 nil
上调用 avatar?
。
最可能的原因是您的固定装置中有一个没有组织的成员,因此 member.organization
的计算结果为 nil
。
去掉问号
a[href=?]
应该是
a[href]
下面的测试生成错误消息
ActionView::Template::Error: undefined method `avatar?' for nil:NilClass
app/views/members/index.html.erb:25:in `block in _app_views_members_index_html_erb___1876261578458959373_9315040'
app/views/members/index.html.erb:22:in `_app_views_members_index_html_erb___1876261578458959373_9315040'
test/integration/site_layout_test.rb:50:in `block in <class:SiteLayoutTest>'
但在开发中它似乎有效。头像显示是否存在。有人知道原因吗?
测试:
get users_path
assert_template 'users/index'
User.paginate(page: 1).each do |user|
assert_select 'a[href=?]', user_path(user)
get organizations_path
assert_template 'organizations/index'
Organization.paginate(page: 1).each do |organization|
assert_select 'a[href=?]', organization_path(organization)
end
get members_path # THIS IS LINE 50!!
assert_template 'members/index'
Member.paginate(page: 1).each do |member|
assert_select 'a[href=?]', member_path(member)
end
会员索引视图包括:
<% @members.each do |member| %> # THIS IS LINE 22!!
<tr>
<td>
<% if member.organization.avatar? %> # THIS IS LINE 25!!
<%= link_to image_tag(member.organization.avatar.url, alt: "Profile"), member_path(member) %> <%= member.username %>
<% else %>
<%= link_to image_tag("profile.gif", alt: "Profile"), member_path(member) %> <%= member.username %>
<% end %>
</td>
<td><%= member.fullname %></td>
etc...
此错误表示您已尝试在 nil
上调用 avatar?
。
最可能的原因是您的固定装置中有一个没有组织的成员,因此 member.organization
的计算结果为 nil
。
去掉问号
a[href=?]
应该是
a[href]