4
我遇到了RSpec的腳手架測試問題,其中any_instance.should_receive正在拋出一個錯誤:失敗/錯誤:無法從回溯中找到匹配的線 正確一個實例應該已經收到以下消息(一個或多個),但沒有:Rspec any_instance should_receive應該收到以下消息,但沒有
的update_attributesRspec的代碼,用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
控制器代碼:
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
考慮到我打印出在我的控制器更新的對象,它是正確的,爲什麼測試失敗?
謝謝!
唉。簡單的錯別字吸吮。我還必須將我的rspec代碼更改爲: Acquisition.any_instance.should_receive(:update_attributes!)。(「person_id」=>'10'),但是這幫助我計算出其餘部分。謝謝。 – 2013-03-01 01:17:55