我对 rspec 和inherited_resources 都是新手。我有一个简单的资源,联系人,它有一个名称字段。控制器没有特殊功能。
class ContactsController < InheritedResources::Base
actions :all, :except => [:show]
end
我已经使用 mock_model 编写了创建和索引的规范。但是,在更新时使用mock_model时,放置时找不到联系人。所以,我转而使用真实模型:
describe "PUT update" do
let(:contact) { Contact.create(:name => "test 1") }
it "edits the contact" do
contact.name = "#{contact.name}-edited"
end
context "when the contact updates successfully" do
before do
contact.stub(:save).and_return(true)
end
it "redirects to the Contacts index" do
put :update, :id => contact.id, :contact => contact
#assigns[:contact].name = "test 1 - edited"
response.should redirect_to(:action => "index")
end
end
context "when the contact fails to save" do
before do
contact.stub(:save).and_return(false)
contact.stub(:update_attributes).and_return(false)
contact.stub(:errors).and_return(:errors => { :anything => "anything" })
end
it "renders the edit template" do
put :update, :id => contact.id, :contact => contact
response.should render_template :edit
end
end
end
我收到以下错误:
Failures:
1) ContactsController PUT update when the contact fails to save renders the edit template
Failure/Error: response.should render_template :edit
Expected block to return true value.
# ./spec/controllers/contacts_controller_spec.rb:82:in `block (4 levels) in <top (required)>'
当我检查 status_code 时,它是一个 302 重定向到 /contacts/:id。
我究竟做错了什么?