2011-01-19 12 views
0

私はRSpecのでは初心者だけど、私はレールのコードで私のルビーRSpecでこのアクションをどのようにテストしますか?

def create 
    @article = current_user.articles.build params[:article] 
    if @article.save 
    redirect_to articles_path, :notice => 'Article saved successfully!' 
    else 
    render :new 
    end 
end 

どのようにRSpecの中にこのアクションをテストするだろうにこのコントローラを持っていますか?

は、あなたがそれについて知りたいん何

+2

ありがとう? – Zinc

+0

current_userオブジェクトをモックするにはどうすればよいですか? – gkrdvl

+2

ご質問を更新してください。 – shingara

答えて

6
describe "POST 'create'" do 
    let(:article) { mock_model(Article) } 

    before(:each) do 
     controller.stub_chain(:current_user,:articles,:build) { article } 
    end 

    context "success" do 
     before(:each) do 
     article.should_receive(:save).and_return(true) 
     post :create 
     end 

     it "sets flash[:notice]" do 
     flash[:notice].should == "Article saved successfully!" 
     end 

     it "redirects to articles_path" do 
     response.should redirect_to(articles_path) 
     end 

    end 

    context "failure" do 
     before(:each) do 
     article.should_receive(:save).and_return(false) 
     post :create 
     end 

     it "assigns @article" do 
     assigns(:article).should == article 
     end 

     it "renders new" do 
     response.should render_template('new') 
     end 
    end 

    end 
+0

Wauuuhhh、ありがとう! – gkrdvl

関連する問題