2013-01-21 5 views
1

메서드가 존재하는지 확인하기 위해 몇 가지 특별한 스텁 메서드 stub_checkstub_chain_check을 만들고 싶습니다. 예를 들어메서드가 Rspec에 있는지 확인하는 새 스텁 메서드를 만듭니다.

:

#spec/controllers/payments_controller_spec.rb` 

describe PaymentsController do 
    it "makes a payment" do 
    # Ensure method exists, Payment.new.respond_to?(:pay) 
    # the API of Payment can change and tests will pass 
    raise "Stubbing wrong method Payment#pay method doesn't exists" unless Payment.new.respond_to?(:pay) 
    Payment.any_instance.stub(pay: true) # We can stub method now 
    # Code... 
    end 
end 

하지만 Payment.stub_check(pay: true)

답변

0

당신은 당신의 spec_helper.rb 파일에 도우미를 만들 수 있습니다처럼 somwthing을하고 싶습니다 :

def stub_check(resource, method, value, message) 
    raise message unless resource.new.respond_to?(method) 
    resource.any_instance.stub(method => value) 
end 

당신이 그것을 호출

stub_check(Payment, :pay, true, 'Stubbing wrong method Payment#pay method doesn't exists') 

EDIT : 스텁처럼 작동 시키려면 mocka 나 사용하고있는 matchers를 수정해야합니다 ("any_instance"메소드 때문에 mocka가 될 것 같습니다)

+0

나는 그것에 대해 생각해 왔습니다. resourse.new.respond_to를 사용하는 좋은 아이디어가 아닌가? 메소드, 더 나은 resource.method_defined입니까? 방법,하지만 Payment.stub_check하고 싶습니다. –