Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an RSpec test for a simple PUT update?

I'm trying to solidify my understanding of rails and the BDD workflow, so I wanted to start small by creating one of those mini-blogs, but with rspec. Right now I have an ArticlesController and Article model, and associated rspec files. Article is very simple, has just title:string and content:text, and the ArticlesController is RESTful - although I hand wrote the MCV for Article, it's basically the same as if I used a scaffold to create it.

However I don't really know what I'm doing when it comes to writing a test in rspec for the PUT update. I'm using Factory Girl to create the article object, and so far my code looks like:

#factories.rb FactoryGirl.define do   factory :article do   title "a title"   content "hello world" end  #articles_controller_spec.rb before(:each) do   @article = Factory(:article) end  describe "PUT 'update/:id'" do   it "allows an article to be updated" do     @attr = { :title => "new title", :content => "new content" }     put :update, :id => @article.id, :article => @attr     response.should be_successful   end end 

However I keep getting:

Failures: 1) ArticlesController PUT 'update/:id' allows an article to be updated    Failure/Error: response.should be_successful      expected successful? to return true, got false 

What am I doing wrong? And am I using the right tools? When I run my test server, New, Edit, Destroy all work as I would expect them to, so I'm guessing this is a problem with my understanding of RSpec. Let me know if I'm wrong - thanks!

like image 617
oort Avatar asked Feb 10 '12 05:02

oort


People also ask

How do I run a specific test in RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

How do I run a RSpec test in terminal?

Open your terminal, cd into the project directory, and run rspec spec . The spec is the folder in which rspec will find the tests. You should see output saying something about “uninitialized constant Object::Book”; this just means there's no Book class.

What RSpec method is used to create an example?

The it Keyword. The word it is another RSpec keyword which is used to define an “Example”. An example is basically a test or a test case. Again, like describe and context, it accepts both class name and string arguments and should be used with a block argument, designated with do/end.

What type of testing is RSpec?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.


1 Answers

You forgot to .reload your @article, and on update action your response most likely perform redirect, so

RSpec 2:

describe "PUT update/:id" do   let(:attr) do      { :title => 'new title', :content => 'new content' }   end    before(:each) do     put :update, :id => @article.id, :article => attr     @article.reload   end    it { response.should redirect_to(@article) }   it { @article.title.should eql attr[:title] }   it { @article.content.should eql attr[:content] } end 

Rspec 3:

describe "PUT update/:id" do   let(:attr) do      { :title => 'new title', :content => 'new content' }   end    before(:each) do     put :update, :id => @article.id, :article => attr     @article.reload   end    it { expect(response).to redirect_to(@article) }   it { expect(@article.title).to eql attr[:title] }   it { expect(@article.content).to eql attr[:content] } end 
like image 194
Rustam A. Gasanov Avatar answered Sep 23 '22 16:09

Rustam A. Gasanov