为什么 rspec 在来自基本 rspec put 更新的以下 rspec 消息中说“已收到:0 次”,如下所示?
Failures:
1) KeywordsController PUT update with valid params updates the requested keyword
Failure/Error: Keyword.any_instance.should_receive(:update_attributes).with({:id => keyword.id, :keyword => {:name => 'keyword name changed'}})
(#<Mocha::ClassMethods::AnyInstance:0x007fc90bfcc7c8>).update_attributes({:id=>853, :keyword=>{:name=>"keyword name changed"}})
expected: 1 time with arguments: ({:id=>853, :keyword=>{:name=>"keyword name changed"}})
received: 0 times with arguments: ({:id=>853, :keyword=>{:name=>"keyword name changed"}})
# ./spec/controllers/keywords_controller_spec.rb:121:in `block (4 levels) in <top (required)>'
关键字控制器规格.rb
describe KeywordsController do
describe "PUT update" do
context "with valid params" do
it "updates the requested keyword" do
keyword = create(:keyword)
Keyword.any_instance.should_receive(:update_attributes).with({:id => keyword.id, :keyword => {:name => 'keyword name changed'}})
put :update, {:id => keyword.id, :keyword => {:name => 'keyword name changed'}}, valid_session
end
end
end
关键字控制器.rb:
class KeywordsController < ApplicationController
def update
@keyword = Keyword.find(params[:id])
respond_to do |format|
if @keyword.update_attributes(params[:keyword])
flash[:notice] = t('controllers.keywords.update.success.keyword_updated')
format.json { render :json => @keyword.to_json, :status => 200 }
format.xml { head :ok }
format.html { redirect_to(edit_keyword_path(@keyword)) }
else
format.json { render :text => "Could not update keyword.", :status => :unprocessable_entity }
format.xml { render :xml => @keyword.errors, :status => :unprocessable_entity }
format.html { render :action => :edit, :status => :unprocessable_entity }
end
end
end
end