Reputation: 127
I am using Mocha library with test unit. https://github.com/freerange/mocha
Here is my controller action
def update_purchase_state
current_state = @purchase.aasm_state
@purchase.update_attribute :aasm_state, params[:purchase_state]
flash[:notice] = "successfully update the purchase state from '#{current_state}' to '#{params[:purchase_state]}'"
redirect_to home_admin_purchase_editor_path(purchase_id: @purchase)
end
Here is my controller test
should "update the purchase's state" do
PurchaseEditor::Purchase.any_instance.expects(:aasm_state).returns("paid")
PurchaseEditor::Purchase.any_instance.expects(:update_attribute)
post :update_purchase_state, purchase_id: "1", purchase_state: "refunded"
assert_response 200
assert_match /successfully update the purchase state/, flash[:notice]
end
The error I am getting is undef method aasm_state for nil class. I am confused bc I thought I was mocking it out with .any_instance. I have also tried .stubs as well with no luck.
Upvotes: 0
Views: 204
Reputation: 29429
.any_instance
will work for instances of Purchase
, but @purchase
is not an instance of Purchase
. It is nil
in this case. You need to address where you assign @purchase
in your controller or you need to artificially assign it yourself from the spec.
Upvotes: 1