Ruby on rails 查找:包含-使用rspec测试控制器
我在rails应用程序中有一些操作Ruby on rails 查找:包含-使用rspec测试控制器,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我在rails应用程序中有一些操作 def show @issue = Issue.find(params[:id], :include => [:answers, :comments]) @answers = @issue.answers @comments = @issue.comments respond_to do |format| format.html # show.html.erb format.xml { re
def show
@issue = Issue.find(params[:id], :include => [:answers, :comments])
@answers = @issue.answers
@comments = @issue.comments
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @issue }
end
end
和rspec试验
def mock_issue(stubs={})
@mock_issue ||= mock_model(Issue, stubs)
end
describe "GET show" do
it "assigns the requested issue as @issue" do
@issue = mock_issue
@answers = mock("answers")
@comments = mock("comments")
Issue.stub(:find).with("37").and_return(@issue)
@issue.stub(:answers).and_return([@answers])
@issue.stub(:comments).and_return([@comments])
get :show, :id => "37"
assigns[:issue].should equal(@issue)
end
end
在尝试运行以下测试时,我看到错误
NoMethodError in 'IssuesController GET show assigns the requested issue as @issue'
undefined method `find' for #<Class:0x105dc4a50>
“IssuesController GET show”中的NoMethodError将请求的问题分配为@issue
未定义的方法“查找”#
但是如果没有:include=>[:answers,:comments]它就可以正常工作。你能告诉我吗?有没有办法存根:包括?哦,我可以回答我的问题:)
Issue.stub(:find).with("37", :include => [:answers, :comments]).and_return(@issue)