2012-02-10 53 views
26

我试图巩固我对rails和BDD工作流程的理解,所以我想通过创建这些迷你博客中的一个来开始小型项目,但是使用rspec。现在我有一个ArticlesController和Article模型,和相关的rspec文件。文章非常简单,只有title:string和content:text,ArticlesController是RESTful - 虽然我手写了MCV for Article,但它基本上与我使用脚手架创建它相同。如何为简单的PUT更新编写RSpec测试?

然而,我并不真正知道自己在做什么,因为在rspec中为PUT更新编写测试。我使用工厂女孩以创建该项目的对象,到目前为止,我的代码如下所示:

#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 

不过,我不断收到:

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 

我在做什么错?我是否正在使用正确的工具?当我运行我的测试服务器时,新建,编辑,销毁所有工作,因为我期望他们,所以我猜这是我对RSpec的理解问题。让我知道如果我错了 - 谢谢!

+0

您的控制器和模型是什么样的? – 2012-02-10 05:54:23

回答

51

你忘了.reload@article,并在update行动的响应最有可能进行重定向,所以

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 
+0

此外,新的匹配器语法会将其作为@ article.title.should eq @attr [:title] – engineerDave 2013-05-17 17:51:05

+1

祝福你美丽的小灵魂。我在我的智慧结束。 – Matt 2014-02-24 15:50:20

+3

较新的语法应该是:'expect(@ article.title).to eq @attr [:title]'。 – Nick 2014-07-09 18:43:07

1
FactoryGirl.define :article do 
    title "a title" 
    content "hello world" 
end 

before(:each) do 
    @article = Factory(:article) 
end 

it "should re-render edit template on failed update" do 
    @attr = { :title => "", :content => "new content" } 
    put :update, :id => @article.id, :article => @attr 

    flash[:notice].should be_nil 
    response.should render_template('edit') 
end 

it "should redirect to index with a notice on successful update" do 
    @attr = { :title => "new title", :content => "new content" } 
    put :update, :id => @article.id, :article => @attr 

    assigns[:article].should_not be_new_record 
    flash[:notice].should_not be_nil 
    response.should redirect_to(:action => 'index') 
end 
+0

是否可以使用POST进行更新? – KnownColor 2014-01-17 08:58:41

+1

默认情况下,@KnownColor更新是放置请求,但您始终可以覆盖它。 – 2014-01-19 17:55:56

5

当你正在做一个PUT :update记住,你是编辑现有的模式,你需要在put打电话。只需传递您的@article并更新属性如下。

describe "PUT 'update/:id'" do 
    it "allows an article to be updated" do 
    put :update, :id => @article.id, :article => @article.attributes = { :title => "new title", :content => "new content" } 
    response.should be_successful 
    end 
end 
0

我喜欢测试更新方法的方式是确保updated_at时间比以前更长。当你这样做时,你可以改变整个实例变量的内容,并检查是否所有内容都已更新。例如:

describe "PUT 'update/:id'" do 
    it "allows an article to be updated" do 
    prev_updated_at = @article.updated_at 
    @attr = { :title => "new title", :content => "new content" } 
    put :update, :id => @article.id, :article => @attr 
    @article.reload 
    @article.updated_at.should != prev_updated_at 
    end 
end 
+0

你的代码似乎断言时代是一样的,没有不同,如你所描述的。 – KnownColor 2014-01-17 08:58:06