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
精彩评论