【问题标题】:ActionController::UnknownFormat in controller rspec after rails 4 upgradeRails 4 升级后控制器 rspec 中的 ActionController::UnknownFormat
【发布时间】:2014-08-14 22:20:33
【问题描述】:

我在这个问题上纠结了一天。这是控制器的样子:

class HarvestSchedulesController < ApplicationController

  before_filter :authenticate_user!

  respond_to :json

  def show
    @harvest_schedule = HarvestSchedule.find(params[:id])
    respond_with @harvest_schedule
  end    
end

和规格:

let(:schedule) { mock_model(HarvestSchedule).as_null_object }

describe "GET show" do
   before(:each) do
     HarvestSchedule.stub(:find).with("1") { schedule }
   end

   it "finds the harvest schedule" do
     HarvestSchedule.should_receive(:find).with("1") { schedule }
     get :show, id: 1
     assigns(:harvest_schedule).should eq schedule
   end
end

当我运行规范时,错误是:

Failure/Error: HarvestSchedule.should_receive(:find).with("1") { schedule }
ActionController::UnknownFormat:
  ActionController::UnknownFormat
# ./app/controllers/harvest_schedules_controller.rb:29:in `show'
# ./spec/controllers/harvest_schedules_controller_spec.rb:41:in `block (3 levels) in <top (required)>'

我根本看不出控制器或规格有任何问题。该应用程序之前使用的是 Rails 3.2.12,并且所有规范都通过了。只有在我将 Rails 版本升级到 4.1.4 后才会出现此错误。我正在使用 rspec 2.14.1。有没有人遇到过类似的问题?

干杯

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 rspec-rails


    【解决方案1】:

    您已在此处指定该操作仅响应 JSON 请求:

    respond_to :json
    

    因此,您还需要在规范中请求 JSON 响应:

    get :show, id: 1, format: 'json'
    

    【讨论】:

    • 嗨@infused,我试过了,但新错误是undefined method authenticate' for nil:NilClass`。我想知道它是否是 Rails 4 问题
    • 我想你可能还需要添加params: { id: 1 }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多