2010-06-04 65 views
0

我有一些動作在我的Rails應用程序找到:包括 - 測試控制器與rspec的

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 => [:answers,:comments] it wo很好。你能告訴我 - 有什麼方法來存根:包括?

回答

1

哦,我可以回答我的問題:)

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