我試圖測試當調用控制器的動作時是否調用某個方法。期望對象在控制器中使用Rspec接收方法
describe Admin::CartProductDesignsController, :type => :controller do
let!(:personalized_cart_product) { create :personalized_cart_product}
let!(:cart_product_design) { create :cart_product_design, quantity: 100, personalized_cart_product: personalized_cart_product }
describe "PATCH #update" do
it "generates the production sheet" do
expect_any_instance_of(CartProductDesign).to receive(:some_method)
patch :update, cart_product_design: {quantity: "20"}, cart_product_id: personalized_cart_product.id, id: cart_product_design , format: 'js'
end
end
end
class Admin::CartProductDesignsController < Admin::ApplicationController
inherit_resources
actions :create, :update, :destroy
respond_to :html, :js
def update
@cart_product_design.some_method
update! do |success, failure|
success.html { redirect_to [:admin, @cart_product_design.order] }
failure.html { redirect_to [:admin, @cart_product_design.order], alert: @cart_product_design.errors.full_messages.first }
end
end
end
,當我的RSpec運行這給了這個錯誤:
1) Admin::CartProductDesignsController PATCH #update generates the production sheet
Failure/Error: Unable to find matching line from backtrace
Exactly one instance should have received the following message(s) but didn't: some_method
爲什麼失敗? 不知何故補丁請求一個黑盒,它只是返回一些值,並不在乎它被稱爲內部?提前
嘗試測試其他html,而不是調用方法 – 2014-11-21 12:37:19
我想測試的方法不會生成任何html。 – hcarreras 2014-11-21 12:40:53
然後只是測試方法工作的結果 – 2014-11-21 12:43:10