I'm using RSpec for testing a SessionsController I am overriding from Devise in my Rails application:
class Users::SessionsController < Devise::SessionsController
The sign_in method renders a jbuilder view:
render '/users/sign_in', status: :ok
I get the expected result when testing the view using curl
. But I haven't been able to make this work with my specs. I get the following error:
Failure/Error: post '/users/sign_in', @data, format: 'json'
ActionView::MissingTemplate:
Missing template users/sign_in with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :haml]}. Searched in:
The jbuilder views are working for other specs by using render_views
. But when I add render_views
to this spec, I get:
`method_missing': undefined local variable or method `render_views' for RSpec::ExampleGroups::UserEdit:Class (NameError)
The difference between the specs where render_views
works and this spec is the type. It's working here:
describe SomeController, type: :controller do
But not on this one:
describe 'User edit', type: :request do
It's a request type since I'm testing the API response. As I said, I get the expected JSON view using curl
from the command line, but it doesn't work in the tests. I must be missing some small detail or doing something completely wrong.
I also tried adding this to Rspec.configure but it didn't change anything:
config.render_views
Edit:
I must add the test pass when using: render json: { stuff }.to_json, status: :ok
in the controller's response. This started failing when I refactored this into a jbuilder view.
Regarding the format, I'm using the 'simple_token_authentication' gem and passing this info to each request in the spec:
{
'X-User-Email' => user.email,
'X-User-Token' => user.authentication_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}
I had format: :json
in other tests and didn't make it work:
post '/users/sign_in', @data, format: :json
So setting format: :json as a value in the second parameter made things work.