Stubbing a 500 error



我遇到了webmocks存根的问题。

这是一个Rails4应用程序,使用design/cacan进行身份验证和授权。我正在使用RSpec编写测试。

我有一个(由于这篇文章的原因而简化了!)我想运行的测试。

require 'rails_helper'
RSpec.describe ApiChecksController, type: :controller do
  include Devise::TestHelpers
  let(:user)          { FactoryGirl.create :user }
  let(:api_params) do
    {
      param_1: 'VALUE',
      param_2: '1980-01-01',
      param_3: 'AA123',
      param_4: "#{Date.today}"
    }
  end
  context 'logged in as standard user' do
  describe 'POST #lookup' do
      context 'displays error' do
        it 'when 500 returned' do
          WebMock.disable_net_connect!(allow: 'codeclimate.com')
          sign_in user
          stub_request(:post, "#{ENV['API_PROXY']}/api/checks").
            to_return(status: [500, "Internal Server Error"])
          post(:lookup, api_check: api_params)
          expect(response.status).to eq(500)
        end
      end
    end
  end
end

在完整的测试套件中,expect语句以上的所有内容都是使用let或set In before块设置的。我试着把它提炼成最小的选项,但测试仍然失败。

问题

我在等

stub_request(:post, "#{ENV['API_PROXY']}/api/checks").
  to_return(status: [500, "Internal Server Error"])

总是返回500状态响应,但它正在返回200。

我的期望正确吗?这就是webmock的调用方式吗?

您需要将查询参数添加到stub_request方法中,您可以将其更改为如下

stub_request(:post, "#{ENV['API_PROXY']}/api/checks")
  .with(query: {api_check: api_params})
  .to_return(status: [500, "Internal Server Error"])

最新更新