在我的SubscriptionsController
我有:
# DELETE /subscription # {{{
def destroy
@subscription = current_user.subscriptions.find params[:id]
@subscription.cancel!
redirect_to subscriptions_path, :notice => "Abonnement beendet."
end# }}}
current_user.subscriptions.find params[:id]
在我的 Controller-Specs中模拟的正确方法是什么?
目前我正在我的前块中尝试这个。
double(Subscription)
controller.current_user.stub!(:subscriptions).and_return(Subscription)
Subscription.stub!(:find).and_return(subscription)
但这似乎没有按预期工作,因为我的 RSpec-Expectations 不起作用。
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
这个块总是失败,因为should_receive
期望没有得到满足:
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)>'
# '
此外,如果有人在评论中删除有关存根、嘲笑的详尽文章的好链接,我会非常高兴。仍然(显然)没有掌握这一点。