0

为什么 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
4

2 回答 2

0

只是一个猜测,但由于您在创建关键字实例后设置了期望,因此可能没有设置期望。如果Keyword.find()将该对象从缓存中拉出,而不是重新创建它,它将失败。

尝试这个:

describe KeywordsController do
  describe "PUT update" do
    context "with valid params" do
      it "updates the requested keyword" do
        Keyword.any_instance.should_receive(:update_attributes).with({:id => keyword.id, :keyword => {:name => 'keyword name changed'}})
        keyword = create(:keyword)
        put :update, {:id => keyword.id, :keyword => {:name => 'keyword name changed'}}, valid_session
      end
    end
end

或者,将期望移到对象本身上(同样,这实际上仅在从缓存中获取时):

describe KeywordsController do
  describe "PUT update" do
    context "with valid params" do
      it "updates the requested keyword" do
        keyword = create(:keyword)
        keyword.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

值得一试!

于 2013-10-09T18:27:07.017 回答
0

我有同样的问题。就我而言,使用expect_any_instance_of(Model)而不是Model.any_instance通过测试。我仍然无法弄清楚为什么。

希望这有帮助。

describe KeywordsController do
  describe "PUT update" do
    context "with valid params" do
      it "updates the requested keyword" do
        keyword = create(:keyword)
        expect_any_instance_of(Keyword).to 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
于 2014-02-21T02:40:29.637 回答