在RSpec

时间:2016-05-18 17:10:25

标签: ruby-on-rails ruby rspec rest-client

我正在尝试测试' ApiClient.do_request'发送正确的参数并调用' RestClient :: Request.execute。'但是,我的以下规格并没有按预期工作。 RSpec无法正确调用' RestClient :: Request.execute,'作为方法的反应'变量保持为零,这就是为什么我在调用JSON.parse(response)时得到下面的TypeError。对我来说很奇怪的是,在Rails控制台中完成这个过程时我没有任何问题。我确定这里有一些显而易见的东西我在这里找不到......为什么会发生这种情况的想法,或者有关如何更有效地测试这个问题的建议?

型号:

class ApiClient < ActiveRecord::Base

  BASE_PATH = "http://api.bandsintown.com/artists/"
  APP_ID = ENV["APP_ID"]

  def do_request(method:, base_url:BASE_PATH, app_id:APP_ID, url:, format:"json", options: nil)
    response = RestClient::Request.execute(method: method.to_sym, 
                                           url: "#{base_url}#{url}.#{format}?api_version=2.0&app_id=#{app_id}#{options}", 
                                           timeout: 10)

    JSON.parse(response)
  end

规格:

describe ApiClient do

  describe "do_request" do

    context "when all required arguments are present" do
      it "RestClient executes the request" do
        @test_client = ApiClient.new

        expect(RestClient::Request).to receive(:execute).with(:method=>:get, :url=> "http://api.bandsintown.com/artists/Damien%20Jurado/events/search.json?api_version=2.0&app_id=ShowBoatTest&location=San+Diego,CA&radius=15", :timeout=>10)

        @test_client.do_request(method:"get", app_id:"ShowBoatTest",url:"Damien%20Jurado/events/search",options:"&location=San+Diego,CA&radius=15")
      end
    end
  end

RSpec错误:

  1) ApiClient do_request when all required arguments are present RestClient executes the request
 Failure/Error: JSON.parse(response)

 TypeError:
   no implicit conversion of nil into String
 # /Users/slamflipstrom/.rvm/gems/ruby-2.1.5/gems/json-1.8.3/lib/json/common.rb:155:in `initialize'
 # /Users/slamflipstrom/.rvm/gems/ruby-2.1.5/gems/json-1.8.3/lib/json/common.rb:155:in `new'
 # /Users/slamflipstrom/.rvm/gems/ruby-2.1.5/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
 # ./app/models/api_client.rb:12:in `do_request'
 # ./spec/models/api_client_spec.rb:13:in `block (4 levels) in <top (required)>'

1 个答案:

答案 0 :(得分:1)

不考虑api_version=2.0&app_id=ShowBoat课程中的ApiClient等语法错误,问题似乎是由RestClient::Request.execute请求返回nil造成的。

调试或检查response的值,并确保它不是nil

class ApiClient < ActiveRecord::Base

  BASE_PATH = "http://api.bandsintown.com/artists/"
  APP_ID = ENV["APP_ID"]

  def do_request(method:, base_url:BASE_PATH, app_id:APP_ID, url:, format:"json", options: nil)
    response = RestClient::Request.execute(method: method.to_sym, 
                                           url: "#{base_url}#{url}.#{format}?api_version=2.0&app_id=#{app_id}#{options}", 
                                           timeout: 10)
    puts response.inspect
    response ? JSON.parse(response) : []
  end

另一方面,您正在测试ApiClient类的实现。换句话说,您正在测试该类是否以特定方式在内部调用其他类。如果您决定在稍后的某个时候用HTTParty替换RestClient,那么您的测试将失败(即使它们不应该,因为您的类仍在正常工作)。

因此,测试类的实现会使您的测试“变得脆弱”。相反,给出输入并测试预期的正确输出。

describe ApiClient do
  context "with all required arguments" do
    describe "#do_request" do
      it "returns object in correct format" do
        response = subject.do_request(method:"get", app_id:"ShowBoatTest",url:"Damien%20Jurado/events/search",options:"&location=San+Diego,CA&radius=15")
        expect(response.body).to include(title: "Damien Jurado @ The Casbah in San Diego, CA")
        # etc.
      end
    end
  end
end