Im는 tdd 및 스터 빙에 새로운 기능입니다. 내가 그 메소드 내의 어떤 코드라도 실행되지 않는다고 추정하는 방법을 스텁했을 때? 예외를 발생시키는 메서드를 위조하려고 시도했지만 내 테스트의 결과는 해당 메서드의 코드가 무시되기보다는 실행 중임을 나타냅니다.mocha가 코드를 스텁 (Rails)에서 실행합니까?
아무도 설명 할 수 있습니까?
내 스터 빙은
@logged_in_user.subscription.stubs(:stop_auto_renew).raises(:RuntimeError)
및 상황에
test "canceling subscription should handle exception raised by spreedly" do
login_larry_active
@logged_in_user.subscription.stubs(:stop_auto_renew).raises(:RuntimeError)
delete :destroy, {:user_id => @logged_in_user.id}
assert flash[:notice]
assert_redirected_to :controller => :dashboard, :action => :welcome
end
이에 스텁하려고 내 모델 메신저의 방법입니다. Spreedly :: 구독자는 일반적으로 웹에서 물건을 가져옵니다.
class Subscription < ActiveRecord::Base
protected
def stop_auto_renew
Spreedly::Subscriber.find(self.user.id).stop_auto_renew()
end
end
-
class SubscriptionsController < ApplicationController
def destroy
user = User.find(params[:user_id])
if user
begin
user.subscription.stop_auto_renew
rescue RuntimeError
#log something and email admin
ldb("Spreedly cant process stop_auto_renew for user id: #{user.id}")
#email admin
end
flash[:notice] = "You have successfully cancelled your subscription and will take effect at the end of your current billing month"
end
redirect_to :controller => :dashboard, :action => :welcome
end
end
및 테스트 오류가
4) Error:
test_canceling_subscription_should_handle_exception_raised_by_spreedly(SubscriptionsControllerTest):
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.stop_auto_renew
app/models/subscription.rb:18:in `stop_auto_renew'
app/controllers/subscriptions_controller.rb:31:in `destroy'
/test/functional/subscriptions_controller_test.rb:42:in `test_canceling_subscription_should_handle_exception_raised_by_spreedly'
오른쪽. Im mocha를 처음 접했고 그 중요한 any_instance 부분을 놓쳤습니다. 당신의 도움을 주셔서 대단히 감사합니다! – robodisco
도움을 주셔서 감사합니다. LOL을 지불하는 TDD를 따라 잡으십시오. – tsdbrown