开发者

How would you test this action in RSpec?

I'm a newbie in RSpec, I have this controller in my ruby on rails code

def create
  @article = current_user.articles.build params[:article]
  if @article.save
    redirect_to articles_path, :notice => 'A开发者_如何转开发rticle saved successfully!'
  else
    render :new
  end
end

How would you test this action in RSpec ?

Thank you


  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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜