What is the proper way to test 'create' controller actions?
controller, rspec, ruby, ruby-on-rails, ruby-on-rails-3
Solution
How about:
it "creates article" do
article_params = FactoryGirl.attributes_for(:article)
expect { post :create, :article => article_params }.to change(Article, :count).by(1)
end
Problem
I am using Ruby on Rails 3.2.2, Rspec 2.9.0 and RspecRails 2.9.0. I would like to test the `create` controller action but I don't know how to make that the "right"/"proper" way. I "scaffolded" model, controller, view, ... files, so in those files I have the common code generated by Ruby on Rails generators; in my spec file I have: ``` it "assigns @article" do new_article = FactoryGirl.build(:article) Article.should_receive(:new).and_return(new_article) post :create assigns[:article].should eq(new_article) end ``` Maybe, (note: the above code is almost the same as that I use to test the `new` controller action) a better way to test `create` controller actions would be to pass some attribute value during the `post :create` action instead of proceed as I make above, but I don't know how to make that and if it is the "right"/"proper" way to make things. So, what is the proper way to test 'create' controller actions?