我试图巩固我对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的理解问题。让我知道如果我错了 - 谢谢!
您的控制器和模型是什么样的? – 2012-02-10 05:54:23