In my SubscriptionsController I have:
# DELETE /subscription # {{{
def destroy
@subscription = current_user.subscriptions.find params[:id]
@subscription.cancel!
redirect_to subscriptions_path, :notice => "Abonnement beendet."
end# }}}
What is the correct way to mock out current_user.subscriptions.find params[:id] in my Controller-Specs?
Currently I am trying this in my before block.
double(Subscription)
controller.current_user.stub!(:subscriptions).and_return(Subscription)
Subscription.stub!(:find).and_return(subscription)
but this seems not to work as expected because my RSpec-Expectations don’t work.
it "updates the status to canceled" do
sub = Subscription.stub!(:find).and_return(subscription)
sub.stub!(:cancel!)
sub.should_receive :cancel!
delete :destroy, :id => 1
end
this block always fails becaus the should_receive expectation is not met:
1) SubscriptionsController DELETE /subscription/:id updates the status to canceled
Failure/Error: sub.should_receive :cancel!
(#<Proc:0x007fbdec0a2650@/Users/nilsriedemann/.rvm/gems/ruby-1.9.3-p125-perf@bloomy-days/gems/rspec-mocks-2.6.0/lib/rspec/mocks/message_expectation.rb:63 (lambda)>).cancel!(any args)
expected: 1 time
received: 0 times
# ./spec/controllers/subscriptions_controller_spec.rb:38:in
# `block (3 levels) in <top (required)>'
# '
Besides if anyone drops nice links to thorough articles about stubbing, mocking in the comments, i’d be ridiculously happy. Still (obviously) not getting the hang of that.
You’re setting the
should_receiveon Subscription, not on the object thatfindwill return.Something like
is what you are after.
You may also be interested in
stub_chain:sets things up so that
returns
some_result