Rails Minitest ActionView::Template::Error: undefined method for nil:NilClas
Rails Minitest ActionView::Template::Error: undefined method for nil:NilClas
这两天我一直在尝试调试它,但我被卡住了。我有一个视图,我正在尝试在 rails 中进行测试,当我在浏览器中手动测试时,该视图工作正常,但我在控制器测试中不断收到此错误:
ActionView::Template::Error: undefined method "name" for nil:NilClass
Here is the full error message.
这是测试 (test/controllers/quotes_controller_test.rb):
test "should get quotes index as browse" do
get browse_path
assert_response :success
end
我渲染这部分的地方坏了 (views/quotes/browse.html.erb):
<% if @recent_quotes.any? %>
<aside class="recent-quotes browse-quotes col-7">
<%= render @recent_quotes %>
</aside>
<% end %>
部分看起来像这样 (views/quotes/_quote.html.erb):
<blockquote id="quote<%= quote.id %> blockquote">
<small class="text-muted">
<%= link_to quote.topic.name, artist_path(quote.topic) %>
</small>
<p class="mb-0"><%= link_to quote.content, quote_path(quote) %></p>
<footer class="blockquote-footer">
<cite title="Source">
<%= link_to quote.speaker.name, quote.source %>
</cite>
</footer>
</blockquote>
控制器动作如下所示 (controllers/quotes_controller.rb):
def browse
@artists = Artist.all
@recent_quotes = Quote.all.limit(7)
end
同样,在浏览器中一切正常,但我无法通过这个简单的测试。如果我删除它通过的部分,那么路线工作正常。我认为测试只是在第一次调用 quote.topic.name
时寻找 name
方法,但没有找到它。
但是它在浏览器中运行,所以我不知道我在这个测试中做错了什么。
对不起,我的评论不够,所以我会尽力回答。
你的灯具里有什么?您是否为所有 recent_quotes
定义了主题艺术家(带名字)?
夹具绕过所有验证,因此即使您在报价单上有它 class,您也不会收到任何无效夹具的错误
在控制器中你有 @recent_quotes = Quote.all.limit(7)
。
但是在固定装置中,您仅为 5 个引用定义演讲者和主题。
这部分:
<% 30.times do |n| %>
quote_<%= n %>:
user: jordan
content: <%= Faker::Lorem.unique.sentence %>
speaker_id: <%= rand(1..5) %>
topic_id: <%= rand(6..10) %>
created_at: <%= 42.days.ago %>
<% end %>
不起作用,因为您没有 ID 在 1..10
中的艺术家。每次fixture创建实例的时候,ID都不是从1开始设置的,你们美工的ID是503576764
这样的。所以这部分确实创建了引号,但是它们的 topic.name
是 nil,因为该主题不存在。
您至少需要为 7 个灯具手动指定演讲者和主题。或者您需要删除带有 30.times
的部分 - 我不确定您是否真的需要它。通常的做法是在 fixtures.You 中使用 2-3 个实例可以阅读更多关于 fixtures here
或者你也可以将fixtures改成factories,这样可以让你更直观方便的处理测试记录。
还有一件事。在这种情况下,有一个非常简单的调试工具。只需在您的控制器中添加几行:
def browse
@artists = Artist.all
@recent_quotes = Quote.all.limit(7)
@recent_quotes.map do |quote|
# you can add all what you need in this array
p [quote.valid?, quote.errors, quote.speaker, quote.topic]
end
end
和 运行 测试。您将在测试输出中看到所有信息。
这两天我一直在尝试调试它,但我被卡住了。我有一个视图,我正在尝试在 rails 中进行测试,当我在浏览器中手动测试时,该视图工作正常,但我在控制器测试中不断收到此错误:
ActionView::Template::Error: undefined method "name" for nil:NilClass
Here is the full error message.
这是测试 (test/controllers/quotes_controller_test.rb):
test "should get quotes index as browse" do
get browse_path
assert_response :success
end
我渲染这部分的地方坏了 (views/quotes/browse.html.erb):
<% if @recent_quotes.any? %>
<aside class="recent-quotes browse-quotes col-7">
<%= render @recent_quotes %>
</aside>
<% end %>
部分看起来像这样 (views/quotes/_quote.html.erb):
<blockquote id="quote<%= quote.id %> blockquote">
<small class="text-muted">
<%= link_to quote.topic.name, artist_path(quote.topic) %>
</small>
<p class="mb-0"><%= link_to quote.content, quote_path(quote) %></p>
<footer class="blockquote-footer">
<cite title="Source">
<%= link_to quote.speaker.name, quote.source %>
</cite>
</footer>
</blockquote>
控制器动作如下所示 (controllers/quotes_controller.rb):
def browse
@artists = Artist.all
@recent_quotes = Quote.all.limit(7)
end
同样,在浏览器中一切正常,但我无法通过这个简单的测试。如果我删除它通过的部分,那么路线工作正常。我认为测试只是在第一次调用 quote.topic.name
时寻找 name
方法,但没有找到它。
但是它在浏览器中运行,所以我不知道我在这个测试中做错了什么。
对不起,我的评论不够,所以我会尽力回答。
你的灯具里有什么?您是否为所有 recent_quotes
定义了主题艺术家(带名字)?
夹具绕过所有验证,因此即使您在报价单上有它 class,您也不会收到任何无效夹具的错误
在控制器中你有 @recent_quotes = Quote.all.limit(7)
。
但是在固定装置中,您仅为 5 个引用定义演讲者和主题。
这部分:
<% 30.times do |n| %>
quote_<%= n %>:
user: jordan
content: <%= Faker::Lorem.unique.sentence %>
speaker_id: <%= rand(1..5) %>
topic_id: <%= rand(6..10) %>
created_at: <%= 42.days.ago %>
<% end %>
不起作用,因为您没有 ID 在 1..10
中的艺术家。每次fixture创建实例的时候,ID都不是从1开始设置的,你们美工的ID是503576764
这样的。所以这部分确实创建了引号,但是它们的 topic.name
是 nil,因为该主题不存在。
您至少需要为 7 个灯具手动指定演讲者和主题。或者您需要删除带有 30.times
的部分 - 我不确定您是否真的需要它。通常的做法是在 fixtures.You 中使用 2-3 个实例可以阅读更多关于 fixtures here
或者你也可以将fixtures改成factories,这样可以让你更直观方便的处理测试记录。
还有一件事。在这种情况下,有一个非常简单的调试工具。只需在您的控制器中添加几行:
def browse
@artists = Artist.all
@recent_quotes = Quote.all.limit(7)
@recent_quotes.map do |quote|
# you can add all what you need in this array
p [quote.valid?, quote.errors, quote.speaker, quote.topic]
end
end
和 运行 测试。您将在测试输出中看到所有信息。