Den*_*din 5 rspec ruby-on-rails update-attributes
我在与其中any_instance.should_receive抛出一个错误RSpec的支架试验的问题:故障/错误:无法找到回溯整整一个实例匹配行应该已收到以下消息(S),但没有: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
Run Code Online (Sandbox Code Playgroud)
控制器代码:
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
Run Code Online (Sandbox Code Playgroud)
测试输出:
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
Run Code Online (Sandbox Code Playgroud)
鉴于我正在我的控制器中打印出更新的对象并且它是正确的,为什么测试失败?
谢谢!
这可能是您的粘贴中的拼写错误,但如果不是,您的测试应该测试是否update_attributes!已收到,而不是update_attributes
Acquisition.any_instance.should_receive(:update_attributes!)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4111 次 |
| 最近记录: |