葡萄索引的Rails rspec GET请求测试失败-返回空数组

时间:2019-05-29 22:53:25

标签: ruby-on-rails rspec grape

我在grape中创建了一个端点,该端点返回值(以下示例),该端点在输入的shop_code之后将一些值作为JSON返回(以下是swagger输出)-另一个应用程序必须获取此数据。我大摇大摆地对其进行了测试,并且工作得很好,但是我的规格没有起作用,并返回了错误

  

实际收藏包含:[]

这是我第一次使用葡萄,这是我第一次必须对它进行一些测试。

correct swagger output

{
  "data": [
    {
      "id": "1",
      "type": "inquiry",
      "attributes": {
        "reference_id": 1,
        "post_code": "99312",
        "intended_use": "Other",
        "amount": 2000,
        "remaining_time": "2019-07-16"
      }
    }
  ]
}

v1/controller/inquiries/data/index.rb

class Index < Base
          params do
            requires :shop_code, type: String
          end

          helpers do
            def find_inquiries
              shop = Shop.find_by(code: params[:shop_code])
              Inquiry.joins(:shop_offers)
                     .where('shop_offers.shop_suggestion_id = ?', shop)
                     .includes(:process)
            end
          end

          get :export_data do
            skip_authorization

            ::Inquiries::DataSerializer.new(find_inquiries)
          end

规格

describe API::V1::Inquiries::Data::Index, type: :request do
  subject(:call) { get endpoint, params: params }
# tons of 'let' which are not the case in this example
  let(:endpoint) { "/api/v1/inquiries/data?shop_code=#{shop.code}" }
  let(:shop) { create(:shop, code: '10011001')}
  let(:params) { { shop_code: '10011001' } }

  describe 'data' do
    subject(:data) { json_response_body['data'] }

    let(:expected_data) do
      [
        {
          "id": inquiry.id,
          "type": 'inquiry',
          "attributes": {
            "reference_id": inquiry.id,
            "post_code": '33445',
            "intended_use": 'Other',
            "amount": 2000,
            "remaining_time": process.order_date
          }
        }
      ]
    end

    it 'returns expected value' do
      call
      expect(data).to match_array(expected_data)
    end
  end
end

我以为:endpoint中的URL错误,但是大张旗鼓地向我显示了shop_code = 06671403 http://localhost:3000/api/v1/inquiries/data?shop_code=06671403的路径

0 个答案:

没有答案