Rspec:请求规范:ActionController :: UnknownFormat response_to json

时间:2019-06-04 16:47:21

标签: rspec-rails

我有一个控制器,该控制器根据传递给它的日期对象对所有打开的约会进行响应。在我的应用程序内部,提取和响应处理程序包含在按预期工作的JavaScript模块中。

控制器:

class Users::Orders::AppointmentsController < ApplicationController

  def index
    @apps = Appointment.where(pick_up_date: params[:pick_up_date])
    respond_to do |format|
      format.json { render json: @apps.to_json}
    end
  end

end

但是在我的请求规范中,出现以下错误。

规格:

RSpec.describe "Orders JS Module", type: :request do

  context "Fetching open appointments" do
    todays_date = Time.now.strftime("%m/%d/%Y")

    it "Fetches open appointments with valid date format via named path" do
      get users_orders_appointments_path, params: { pick_up_date: todays_date}

      puts response.body

      # expect(response.body).to include("6am - 7am")
    end
  end

错误:

1) Orders JS Module Fetching open appointments Fetches open appointments with valid date format via named path
     Failure/Error:
       respond_to do |format|
         format.json { render json: @apps.to_json}
       end

     ActionController::UnknownFormat:
       ActionController::UnknownFormat
     # ./app/controllers/users/orders/appointments_controller.rb:5:in `index'
     # ./spec/requests/orders_js_spec.rb:9:in `block (3 levels) in <top (required)>'

我发现的几种解决方案,包括在参数上使用,format: :json选项,似乎仅对post请求有效。

有什么我想念的吗?

0 个答案:

没有答案