find with:include - 使用rspec测试控制器

时间:2010-06-04 07:31:49

标签: 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  { 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>

但没有:include =&gt; [:答案,:评论] 它运作正常。你能告诉我 - 有没有办法存根:包括?

1 个答案:

答案 0 :(得分:1)

哦,我可以回答我的问题:)

 Issue.stub(:find).with("37", :include => [:answers, :comments]).and_return(@issue)