[rspec-users] What is the best practices for controller specs?

Guirec Corbel guirec.corbel at gmail.com
Fri Jun 28 15:26:25 UTC 2013


So, you test your controllers in integration and you test the assignation
of the variables. The complete opposite of me.

How many controllers tests do you have and how many time it take?


2013/6/28 Patrick J. Collins <patrick at collinatorstudios.com>

> > Is it good to tests only if the controller render a template or redirect
> to
> > another action?
>
> My typical controller testing style is something like:
>
> describe ArticleController do
>
>   describe "#index" do
>     it "renders the listing" do
>       article = create(:article)
>       get :index
>       response.should be_success
>       response.should_not be_redirect
>       assigns[:articles].should == [article]
>     end
>   end
>
>   describe "#show" do
>     it "shows the record" do
>       article = create(:article)
>       get :show, :id => article.id
>       response.should be_success
>       response.should_not be_redirect
>       assigns[:article].should == article
>     end
>   end
>
>   describe "#create" do
>     it "creates a record" do
>       expect {
>         post :create, :article => { :body => 'hot bod' }
>       }.to change { Article.count }.by(1)
>       response.should redirect_to articles_url
>     end
>
>     it "renders the new template on failure" do
>       Article.any_instance.should_receive(:save).and_return(false)
>       expect {
>         post :create, :article => { :body => 'hot bod' }
>       }.to_not change { Article.count }
>       response.should_not be_redirect
>       response.should render_template(:new)
>       assigns[:article].should be_instance_of(Article)
>       assigns[:article].should be_new_record
>     end
>   end
>
>   # etc...
>
> Patrick J. Collins
> http://collinatorstudios.com
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20130628/97a36d86/attachment.html>


More information about the rspec-users mailing list