Rails 从控制器规范测试 JS 视图

Rails test JS view from controller spec

我有一个响应 js 响应的控制器操作。这个 js 响应实际上呈现了另一个部分。我想要一个控制器测试,以防部分或 js 文件出现错误。

控制器:

class ZonesController < ApplicationController
  def library_zones
  end
end

library_zones.coffee:

$("#zoneSelectionPlaceholder").html("<%= j(render 'library_zones') %>")

_library_zones.html.haml(注意我调用模型 BadName,试图得到一个错误):

= collection_select(:zones, :zone_id, BadName.library_zones, :id, :description, {prompt: 'Zone to Add...'}, {id: 'zoneToAddSelection'})

routes.rb:

resources :zones do
  collection do
    get 'library_zones
  end
end

规格:

RSpec.describe ZonesController, type: :controller do
  describe 'library_zones' do
    it 'renders library_zones.js template' do
      xhr :get, :library_zones
      expect(response).to render_template('library_zones')
      expect(response.status).to eq(200)
    end
  end
end

我的所有规格都通过了。我试过将 haml 解析错误引入 _library_zones.html.haml 文件以及尝试将 js 错误引入 librarys_zones.coffee 文件,但似乎没有什么会导致此测试失败.

默认情况下,控制器规范不会呈现视图,除非您特别启用它:

https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/controller-specs/render-views