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

Most upvoted comments

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.

test "should get index" d 
  get posts_url, as: :json
  assert_response :success
end

cc @kaspth