使用cucumber时出现方法丢失错误(使用rails、webrat和mongomapper)
我正在 Cucumber 中编写一些基本步骤指令,但无法弄清楚为什么它会抛出“方法丢失”错误。这是精确的错误:
undefined method `question' for nil:NilClass (ActionView::Template::Error)
这个错误的根源是我的index.html.erb页面中的这一行:
<p id="question"><%= @question.question %></p>
所以看起来测试认为@question为零。我不知道它为什么这么想。我应该在步骤说明中更改哪些内容?它们是这样的:
Given /^a question$/ do
@question = Question.create!(:question => "here's my question..", :answer => "right_answer", :num => "1")
end
When /^I answer the question$/ do
visit root_path
fill_in "Answer", :with => "Wrong answer"
click_button "Submit answer"
end
第一步指令通过,但第二步指令没有通过。
编辑:这是我的整个index.html.erb页面,以防它有用:
<% title "Questions" %>
<p id="question"><%= @question.question %></p>
<%= form_for @user_answer do |f| %>
<div><%= f.label :answer %></div>
<div><%= f.text_field :answer %></div>
<div id="sign_up_button"><%= f.submit "Submit answer" %></div>
<% end %>
<p><%= link_to "New Question", new_question_path %></p>
编辑:我的问题模型,以防它有用:
class Question
include MongoMapper::Document
key :question, String, :required => true
key :answer, String, :required => true
key :num, String, :required => true
I'm writing a few basic step instructions in Cucumber, and can't figure out why it's throwing up a "method missing" error. Here's the precise error:
undefined method `question' for nil:NilClass (ActionView::Template::Error)
The root of this error is this line from my index.html.erb page:
<p id="question"><%= @question.question %></p>
So it looks like the test thinks that @question is nil. I'm not sure why it thinks this. What should I change in my step instructions? Here they are:
Given /^a question$/ do
@question = Question.create!(:question => "here's my question..", :answer => "right_answer", :num => "1")
end
When /^I answer the question$/ do
visit root_path
fill_in "Answer", :with => "Wrong answer"
click_button "Submit answer"
end
The first step instruction passes, but the second does not.
EDIT: Here's my entire index.html.erb page, in case it's useful:
<% title "Questions" %>
<p id="question"><%= @question.question %></p>
<%= form_for @user_answer do |f| %>
<div><%= f.label :answer %></div>
<div><%= f.text_field :answer %></div>
<div id="sign_up_button"><%= f.submit "Submit answer" %></div>
<% end %>
<p><%= link_to "New Question", new_question_path %></p>
EDIT: My Question model, in case it's useful:
class Question
include MongoMapper::Document
key :question, String, :required => true
key :answer, String, :required => true
key :num, String, :required => true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您正确地向我们提供了堆栈跟踪的摘录,那么您的问题是您没有在控制器中正确设置@question - 您没有在上面发布。其他几个人在他们的回答中提到了这一点,但我对此深信不疑。
您的测试(黄瓜或其他测试)与您遇到的问题无关。
我怀疑如果你访问你的 root_url,你会遇到同样的问题 - 你会得到一个堆栈跟踪和一个错误。如果不确切知道您的路线是什么,我无法确定当您访问 root_url 时您将被发送到哪里。
您的控制器应该使用您的问题对象加载@question(可能通过Question.find()或类似的东西),然后渲染您的index.html.erb页面。因此,控制器中的“索引”操作应该是填写@question 的内容。
请注意,您在这里没有遵循休息原则 - 通常人们有“索引”为您提供事物列表(您的实例中的问题),然后 id 给您一个特定的问题。
我敢打赌,如果你去回答一个特定的问题,它会很好地工作。
也许您的问题是您不应该首先通过访问“root_path”来进行测试?也许你应该走一个特定的“问题”路径?
Assuming you're giving us the excerpt from the stack trace properly, your issue is that you aren't setting @question properly in your controller - which you have not posted above. Several other people have alluded to that in their answers, but I'm quite convinced of it.
Your tests - cucumber or otherwise - have nothing to do with the problem you're having.
I suspect that if you visit your root_url, you will get the same problem - you will get a stack trace and an error. Without knowing exactly what your routes are, I can't be sure where you're being sent when you visit the root_url.
Your controller should be loading up @question with your question object (probably via Question.find() or something like it), then rendering your index.html.erb page. So your 'index' action in your controller ought to be the thing that is filling in your @question.
Note that you're not following rest-ey principles here - usually people have 'indexes' give you lists of things (questions in your instance), then id's give you a particular question.
I bet if you go to a particular question it works fine.
Maybe your problem is that you shouldn't be testing by visiting 'root_path' in the first place? Maybe you should be going to a particular "question's" path?
您对问题模型有任何验证吗?也许您无法像您尝试的那样创建新的问题对象。
Do you have any validations at Question model? Maybe you can't create new question object like you trying.