gpt4 book ai didi

ruby-on-rails - Rspec any_instance should_receive 应该收到以下消息但没有

转载 作者:行者123 更新时间:2023-12-04 21:19:36 26 4
gpt4 key购买 nike

这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the help center .




8年前关闭。




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

Rspec 代码,使用 FactoryGirl 和 strong_parameters:

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

Controller 代码:
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

测试输出:
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

鉴于我正在 Controller 中打印出更新的对象并且它是正确的,为什么测试失败?

谢谢!

最佳答案

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

Acquisition.any_instance.should_receive(:update_attributes!)

关于ruby-on-rails - Rspec any_instance should_receive 应该收到以下消息但没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15148391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com