1
我是rspec和inherited_resources的新手。我有一個簡單的資源,聯繫人,它有一個名稱字段。控制器沒有特殊的功能。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重定向到/聯繫人/:ID。
我在做什麼錯?
這一切都變得清晰。謝謝! – rmw 2011-05-11 16:37:48