rails: Turning on jbuilder for API only apps cause controller test failures
Steps to replicate:
rails new robots --api
cd robots
rails generate scaffold post title:string
rails db:migrate
<Uncomment jbuilder gem from Gemfile>
bundle
rails test
Failures:
PostsControllerTest#test_should_update_post:
ActionView::MissingTemplate: Missing template posts/update, application/update with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
* "/Users/david/Code/demos/robots/app/views"
app/controllers/posts_controller.rb:30:in `update'
test/controllers/posts_controller_test.rb:27:in `block in <class:PostsControllerTest>'
Line 30 has render json: @post
, so its not relying on default rendering. It should just be calling #to_json
on @post
and return that. Shouldn’t matter what rendering engines are installed or not.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 18 (18 by maintainers)
Commits related to this issue
- Depend on ~> 2.5 for jbuilder on new Apps. Based on https://github.com/rails/rails/issues/25183#issuecomment-222554993 — committed to vipulnsward/rails by vipulnsward 8 years ago
- Fix API controller tests by assigning them the encoding type - Fixes #25183. - The `as: :json` feature was added in https://github.com/rails/rails/pull/21671 and recommended to use for JSON endpo... — committed to prathamesh-sonpatki/rails by prathamesh-sonpatki 8 years ago
- Trying to reproduce the issue - Followed steps from https://github.com/rails/rails/issues/25183#issue-157340201. rails new robots --api cd robots rails generate scaffold post title:string rails db:m... — committed to prathamesh-sonpatki/jbuilder-issue by prathamesh-sonpatki 8 years ago
- Bump Gemfile entry jbuilder version jbuilder 2.6.4 is the first version that relaxes the version constraint to allow Rails 6. I also did some more tests in #25183, although not with 2.6.4 explicitl... — committed to aried3r/rails by aried3r 5 years ago
- Bump Gemfile entry jbuilder version jbuilder 2.6.4 is the first version that relaxes the version constraint to allow Rails 6. I also did some more tests in #25183, although not with 2.6.4 explicit... — committed to aried3r/rails by aried3r 5 years ago
See https://github.com/rails/rails/pull/25317#issuecomment-224376800, this issue isn’t fully resolved. There’s a bandaid in place for 5.0.0 though, so I’m moving this to the 5.0.1 milestone.
This is failing because by default the request encoder is HTML - https://github.com/prathamesh-sonpatki/rails/blob/master/actionpack/lib/action_dispatch/testing/integration.rb#L327
We can fix it in following way by generating API controller tests with
as: :json
as follows.cc @kaspth