首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rspec any_instance should_receive应已收到以下消息,但未收到

Rspec any_instance should_receive应已收到以下消息,但未收到
EN

Stack Overflow用户
提问于 2013-03-01 08:35:00
回答 1查看 6.2K关注 0票数 5

我在rspec的脚手架测试中遇到了一个问题,any_instance.should_receive抛出了一个错误:失败/错误:无法从回溯中找到匹配的行只有一个实例应该收到以下消息,但却没有收到: update_attributes

Rspec代码,使用FactoryGirl和strong_parameters:

代码语言:javascript
复制
describe "PUT update" do
   describe "with valid params" do
     it "updates the requested acquisition" do
       puts "starting updates the requested acquisition"
       acquisition = create(:acquisition)
       # Assuming there are no other acquisitions in the database, this
       # specifies that the Acquisition created on the previous line
       # receives the :update_attributes message with whatever params are
       # submitted in the request.
       Acquisition.any_instance.should_receive(:update_attributes).with(:acquisition =>    {:person_id => '10'})
      puts "before the put "
       put :update, :id => acquisition.id, :acquisition => { :person_id => '10'}
       puts "ending the updates the requested acquisition"
     end   
  end
end

控制器代码:

代码语言:javascript
复制
def update
    @acquisition = Acquisition.find(params[:id])
    puts "acquisition is #{@acquisition.inspect}"
    respond_to do |format|
      if @acquisition.update_attributes!(acquisition_params)
        puts "updated the thing! #{@acquisition.inspect}"
        format.html { redirect_to(@acquisition, :notice => 'Acquisition was successfully updated.') }
        format.xml  { head :ok }
      else
        puts "failed to update the thing!"
        format.html { render :action => "edit" }
        format.xml  { render :xml => @acquisition.errors, :status => :unprocessable_entity }
      end
    end
  end

测试输出:

代码语言:javascript
复制
starting updates the requested acquisition
before the put
acquisition is #<Acquisition id: 502, sample_id: 7, person_id: 7, method: nil>
params is acqusition_params is {"person_id"=>"10"}
updated the thing! #<Acquisition id: 502, sample_id: 7, person_id: 10, method: nil>
ending the updates the requested acquisition
F

1) AcquisitionsController PUT update with valid params updates the requested acquisition
 Failure/Error: Unable to find matching line from backtrace
   Exactly one instance should have received the following message(s) but didn't: update_attributes

假设我在控制器中打印出了更新后的对象,而且它是正确的,为什么测试会失败?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-01 09:10:14

这可能是粘贴中的拼写错误,但如果不是,测试应该测试是否接收到update_attributes!,而不是update_attributes

代码语言:javascript
复制
Acquisition.any_instance.should_receive(:update_attributes!)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15148391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档