11

对不起标题,我太沮丧了,现在想不出更好的东西。

我有一个类,Judge它有一个方法#stats。这个stats方法应该向 api 发送一个 GET 请求并获取一些数据作为响应。我正在尝试对此进行测试并存根 stats 方法,以便我不执行实际请求。这是我的测试的样子:

describe Judge do
  describe '.stats' do

    context 'when success' do
      subject { Judge.stats }

      it 'returns stats' do
        allow(Faraday).to receive(:get).and_return('some data')

        expect(subject.status).to eq 200
        expect(subject).to be_success
      end
    end
  end
end

这是我正在测试的课程:

class Judge

  def self.stats
    Faraday.get "some-domain-dot-com/stats"
  end

end

这目前给了我一个错误:Faraday does not implement: get 那么你如何用法拉​​第存根呢?我见过这样的方法:

    stubs = Faraday::Adapter::Test::Stubs.new do |stub|
      stub.get('http://stats-api.com') { [200, {}, 'Lorem ipsum'] }
    end

但我似乎无法以正确的方式应用它。我在这里想念什么?

4

5 回答 5

13

请注意,Faraday.new 返回的是 Faraday::Connection 的实例,而不是 Faraday。所以你可以尝试使用

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")
)

这是一个 Rails 控制台,显示您从 Faraday.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
于 2016-03-25T21:02:52.010 回答
2

法拉第类没有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

然后在您的测试中,您可以设置一个双精度:

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
于 2014-02-26T19:08:40.677 回答
2

来晚了,但如果其他人也是,这对我有用 - 结合上述方法:

  let(:json_data) { File.read Rails.root.join("..", "fixtures", "ror", "501100000267.json") }

  before do
    allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(
      double(Faraday::Response, status: 200, body: json_data, success?: true)
    )
  end
于 2021-03-24T15:12:17.513 回答
1

我遇到了与Faraday::Adapter::Test::Stubs错误相同的问题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
于 2016-03-18T21:48:43.473 回答
1

比使用 更好的方法allow_any_instance_of是为 Faraday 设置默认连接,以便Faraday.get使用您在测试中设置的连接。

例如:

  let(:stubs) { Faraday::Adapter::Test::Stubs.new }
  let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }

  before do
    stubs.get('/maps/api/place/details/json') do |_env|
      [
        200,
        { 'Content-Type': 'application/json' },
        { 'result' => { 'photos' => [] } }.to_json
      ]
    end

    Faraday.default_connection = conn
  end

  after do
    Faraday.default_connection = nil
  end
于 2021-11-15T14:31:01.923 回答