【问题标题】:Rails json API with Rspec -> ActionView::MissingTemplate带有 Rspec 的 Rails json API -> ActionView::MissingTemplate
【发布时间】:2014-11-28 19:09:51
【问题描述】:

所以我下面有一个api

#controllers/api/v4/...rb

this api render jbuilder json under:

#views/api/v4/....json.jbuilder

我有规格:

#spec/controllers/api/v4/..._spec.rb
require 'rails_helper'

RSpec.describe Api::V4::AlbumsController do

  let(:user) { create(:user) }

  before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      sign_in(user)
  end

  describe "GET index" do
    it "returns correct types" do
      get :index, {:format => :json, :user_id => user.id}
      puts @request.body
      puts response.body
      user_response = json_response
      puts user_response
    end
  end
end

一切正常,直到我得到这个:

Failure/Error: get :index, {:format => :json, :user_id => user.id}
  ActionView::MissingTemplate:
   Missing template api/v4/albums/index, api/api/index with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
     * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007ff4721d9f20>"

有人有办法解决这个问题吗?

【问题讨论】:

  • 你支持 html 和 json 响应吗?
  • 我的 api 在他们自己的文件夹 api 中,我在 routes.rb -> 命名空间 :api 中将默认渲染设置为 json,默认值:{format: 'json'} 做。

标签: ruby-on-rails ruby json rspec


【解决方案1】:

我需要在我的 gemfile 中包含 jbuilder...以便它可以呈现 .json.jbuilder 视图。

【讨论】:

    猜你喜欢
    • 2016-08-20
    • 2020-03-03
    • 1970-01-01
    • 2020-09-26
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多