카테고리 없음

[루비] 패러데이로 루비 스터 빙, 작동하지 못함

행복을전해요 2021. 2. 9. 01:59

Faraday.new는 Faraday가 아닌 Faraday :: Connection의 인스턴스를 반환합니다. 그래서 당신은 사용해 볼 수 있습니다

allow_any_instance_of(Faraday::Connection).to receive(:get).and_return("some data")

Faraday :: Connection.get은 문자열 대신 본문 및 상태 코드를 포함하는 응답 객체를 반환해야하므로 질문에 표시된 "일부 데이터"를 반환하는 것이 올바른지 모르겠습니다. 다음과 같이 시도해 볼 수 있습니다.

allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(
   double("response", status: 200, body: "some data")
   )
   

패러데이에서 돌아 오는 클래스를 보여주는 레일 콘솔이 있습니다 .new

$ rails c
Loading development environment (Rails 4.1.5)
2.1.2 :001 > fara = Faraday.new
 => #<Faraday::Connection:0x0000010abcdd28 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v0.9.1"}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x0000010abcd990 @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]>, @url_prefix=#<URI::HTTP:0x0000010abcd378 URL:http:/>, @proxy=nil>
 2.1.2 :002 > fara.class
  => Faraday::Connection
  
-------------------

패러데이 클래스에는 get메서드 가없고 인스턴스 만 있습니다. 클래스 메서드에서 이것을 사용하고 있기 때문에 할 수있는 것은 다음과 같습니다.

class Judge
  def self.stats
      connection.get "some-domain-dot-com/stats"
        end
        
          def self.connection=(val)
              @connection = val
                end
                
                  def self.connection
                      @connection ||= Faraday.new(some stuff to build up connection)
                        end
                        end
                        

그런 다음 테스트에서 double을 설정할 수 있습니다.

let(:connection) { double :connection, get: nil }
before do
  allow(connection).to receive(:get).with("some-domain-dot-com/stats").and_return('some data')
    Judge.connection = connection
    end
    
-------------------

Faraday::Adapter::Test::Stubs.NET 오류와 동일한 문제가 발생 했습니다 Faraday does not implement: get. 다음 stubs과 같이 패러데이 어댑터 로 설정해야하는 것 같습니다 .

  stubs = Faraday::Adapter::Test::Stubs.new do |stub|
    stub.get("some-domain-dot-com/stats") { |env| [200, {}, 'egg'] }
      end
      
        test = Faraday.new do |builder|
            builder.adapter :test, stubs
              end
              
                allow(Faraday).to receive(:new).and_return(test)
                
                  expect(Judge.stats.body).to eq "egg"
                    expect(Judge.stats.status).to eq 200
                    


출처
https://stackoverflow.com/questions/22050338