【问题标题】:rspec testing a controller method, calling a lib wrapper that makes an API callrspec 测试控制器方法,调用进行 API 调用的 lib 包装器
【发布时间】:2016-04-06 17:10:53
【问题描述】:

在应用程序中,用户可以填写 3 个字段(和 1 个隐藏字段)并单击“加载响应”,这将对外部 API 进行 ajax API 调用。对外部 API 的调用位于 lib 的包装器中。此包装器存储用户不需要输入但 API 要求的其他必填字段。

我看过其他一些关于测试 API 调用的帖子,但我很难将它们与这篇文章进行比较并从中学到一些东西,因为有额外的包装器。

基于系统架构测试此 API 的测试是什么样的?

在视图中:

data = ->
  string_ssid = '#{@bar.ssID}'
  {
    approval_code: $('#approval_code').val()
    rejection_code: $('#rejection_code').val()
    rejection_message: $('#rejection_message').val()
    ssid: string_ssid
  }

ajax_call = ->
  $('#load_response').on 'click', ->
    $.ajax
      url: '/fooview/api_response'
      data: data()
      success: (d) ->
        $('#api_response').html '<p>' + JSON.stringify(d) + '</p>'
        return
      error: (d, status, e) ->
        $('#api_response').html '<p>Error: ' + e + ', ' + status + '</p>'
        return
    return
  return

路线:

get '/baz/api_response', to: 'baz#api_response'

控制器:

  def api_response
    render json: BazWrapper.get_api_response(params)
  end

包装器:

module BazWrapper

  @base_url = Enviro.get(:foo) + '/message/json'

  def self.get_api_response(params)

    query_values = {
    api_key: Enviro.get(:foo, :api_key),
    approval_code: params[:approval_code],
    rejection_code: params[:rejection_code],
    rejection_message: params[:rejection_message],
    ssid: params[:ssid],
    (..other required params hard-wired..)
  }

  HTTParty.post(
    URI.encode(@base_url),
    query: query_values
    )
  end
end

【问题讨论】:

  • 我不确定你到底在问什么。
  • 添加了一句话来澄清我在问什么:)

标签: ruby-on-rails ruby json ruby-on-rails-3 rspec


【解决方案1】:

我会使用功能规范在高级别上对此进行测试,并且还会对包装器模块进行单元测试。

为了帮助测试和阻止请求到达真正的 API,我会使用 WebMock gem (https://github.com/bblimke/webmock)。使用此 gem,您将能够存根请求并设置响应和状态代码。

使用 RSpec/Capybara 的功能规范看起来像 -

RSpec.feature 'loads response from Baz', type: :feature do
  scenario 'response if success' do
    stub_request(:any, 'www.example.com').
      to_return(:body => 'blah', :status => 200)

    visit '/my-url'

    fill_in '#approval_code', with: '123'
    # ... fill in other fields ...

    click_button 'Load Response'

    # check the page contains whatever you expect as the stringified JSON
    expect(page).to have_content('blah')
  end

  # then test the failure scenario
end

您可以更进一步并编写一个规范来测试您的包装器是否请求具有预期参数的正确 URL。

再次使用 WebMock,您可以在 WebMock 模块上设置期望值并验证最后一个请求。

例如

describe BazWrapper do
  describe '.get_api_response' do
    let(params) {
      {
        approval_code: 'x'
      }
    }

    before do
      stub_request(:any, 'www.example.com').
        to_return(:body => 'blah', :status => 200)
    end

    it 'requests the data' do
      BazWrapper.get_api_response(params)

      expect(WebMock).to have_requested(:post, 'www.example.com').
        with(query: { approval_code: 'x' })
    end
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多