puntopagos 및 Rest-client를 사용하는 컨트롤러 작업을 테스트하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com//questions/25006798

문제

이름이 POST 작업인 컨트롤러가 있습니다. create.생성 작업에서 나는 푼토파고스 보석 수업 (PuntoPagos::Request)는 나머지 클라이언트 보석 API에 POST를 수행하려면 다음을 수행하십시오.

class SomeController < ApplicationController

  def create
    request = PuntoPagos::Request.new
    response = request.create
    #request.create method (another method deeper, really)
    #does the POST to the API using rest-client gem.

    if response.success?    
      #do something on success
    else
      #do something on error
    end
  end

end

생성 작업을 테스트하기 위해 RSpec을 사용하여 나머지 클라이언트 요청 및 응답을 어떻게 스텁할 수 있습니까?

도움이 되었습니까?

해결책

그냥 스텁 PuntoPagos::Request.new 그리고 계속해서 스터빙을 하세요:

response = double 'response'
response.stub(:success?) { true }
request = double 'request'
request.stub(:create) { response }
PuntoPagos::Request.stub(:new) { request }

성공적인 요청을 위한 것입니다.다시 해봐 success? 돌아오려고 스텁됨 false 해당 지점을 테스트합니다.

작동이 완료되면 다음을 살펴보세요. stub_chain 더 적은 타이핑으로 동일한 작업을 수행합니다.

그렇긴 하지만, PuntoPagos 항목을 더 간단한 인터페이스를 사용하여 별도의 클래스로 추출하는 것이 훨씬 더 나을 것입니다.

class PuntoPagosService
  def self.make_request
    request = PuntoPagos::Request.new
    response = request.create
    response.success?
  end
end

그럼 넌 그냥 할 수 있어

PuntoPagosService.stub(:make_request) { true }

당신의 테스트에서.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top