2011-08-14 3 views
0

컨트롤러에서 업데이트 작업의 실패한 분기를 테스트하려고하는데 테스트에 문제가 있습니다. 이것은 내가 가진 것이며 마지막에는 실패합니다.Rspec을 사용하여 컨트롤러의 업데이트 작업을 테스트하는 데 문제가 있습니다. 무엇을 잘못하고 있습니까?

describe "PUT 'article/:id'" do 
. 
. 
. 
    describe "with invalid params" do 
    it "should find the article and return the object" do 
     Article.stub(:find).with("1").and_return(@article) 
    end 

    it "should update the article with new attributes" do 
     Article.stub(:update_attributes).and_return(false) 
    end 

    it "should render the edit form" do 
     response.should render_template("edit") 
    end 
    end 
end 

왜 마지막 부분이 템플릿 렌더링에 실패했는지에 대한 아이디어가 있습니까?

답변

5

테스트 부분을 잘못 나눕니다. 각 it 호출은 실제로 새로운 예제이며 각 상태 전후에 상태가 재설정됩니다. (당신 정말이 원한다면 당신은 모의 하나를 사용할 수 있지만)

describe "with invalid params" do 
    before do 
    @article = Article.create(valid_params_go_here) 
    end 

    it "should find the article and return the object" do 
    put :update, { :id => @article.id, :article => { :title => "" } } 
    response.should render_template("edit") 
    end 
end 

이런 식으로 그 일을함으로써, @article가 손 전에 설정하고 요청 :

당신이 일을해야하는 것입니다 실제로는 edit 템플릿이 모두 하나의 예제로 렌더링된다는 단언과 함께 동작합니다.

+0

응답 해 주셔서 감사합니다. 그래서 제가 그것들을 하나로 합치면 더 나아질 것입니다. 이것을 테스트하는 올바른 방법입니까? 'it "은 기사를 찾아서"do " ' Article.stub (: find) .with ("1 ") and_return (@article)' 'Article.stub (: update_attributes) 객체를 반환해야합니다. and_return (false)를' '넣어 : 업데이트 {: ID => "1": 기사 => {: 제목 => 전무}}' 'response.should는 render_template ("편집")' ' 끝 ' –

+0

@luis : 예, 그렇게 할 수 있습니다. –

관련 문제