带有RSpec->ActionView::MissingTemplate的rails json.jBuilder API [英] rails json.jbuilder api with rspec -> ActionView::MissingTemplate

查看:0
本文介绍了带有RSpec->ActionView::MissingTemplate的rails json.jBuilder API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails(6.0.0)项目,其中的API路由响应与jBuilder(2.9.1)放在一起。我在使用RSpec-Rils(3.9.0)测试控制器时遇到一个错误,但它抛出了这个错误:

Failure/Error: render :show

     ActionView::MissingTemplate:
       Missing template api/users/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
         * "#<RSpec::Rails::ViewRendering::EmptyTemplateResolver::ResolverDecorator:0x00007efbf0062f50>"
         * "#<RSpec::Rails::ViewRendering::EmptyTemplateResolver::ResolverDecorator:0x00007efbf0062ed8>"
         * "#<RSpec::Rails::ViewRendering::EmptyTemplateResolver::ResolverDecorator:0x00007efbf0062e88>"
     # ./app/controllers/api/users_controller.rb:8:in `create'
     # ./spec/controllers/api/users_controller_spec.rb:7:in `block (4 levels) in <top (required)>'
     # -e:1:in `<main>'

控制器/API/USERS_CONTROLER.rb:

def create
  @user = User.new(user_params)
  if @user.save
    login(@user)
    render :show
  else
    render json: @user.errors.full_messages, status: 422
  end
end

视图/API/USERS/show.json.jBuilder:

json.partial! 'api/users/user', user: @user

routes.rb:

  namespace :api, defaults: { format: :json } do
    resources :users, only: [:create, :index, :update]

USERS_CONTROLLER_SPEC.rb:

it 'validates the presence of username, password, and email' do
  post :create, params: { use_route: 'api/users', user: { username: 'username', password: 'password', email: 'email' } }
  expect(response).to have_http_status 200
end

我在Stackoverflow上看了很久,但没有找到任何对此有帮助的东西。不确定是Rails版本问题,还是我没有正确设置某些内容。尽管我在API命名空间下的routes.rb文件中将默认格式设置为json,但它似乎一直在尝试查找html格式的文件,而不是json。

如有任何帮助,我们将不胜感激!

推荐答案

我知道发布此问题已经有一段时间了,但我要检查两件事:确保告知RSpec配置以呈现视图,并确保以json身份发布/获取。

在规范/rails_helper.rb中

RSpec.configure do |config|
   config.render_views = true
   # the rest of config
end

在规范文件中

post :create, params: { ... }, as: :json  

这篇关于带有RSpec-&gt;ActionView::MissingTemplate的rails json.jBuilder API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆