1

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.

Fernando Briano
  • 7,699
  • 13
  • 58
  • 75
  • Could you paste the test that passes and the test that fails? The detail you may be missing (just a guess) is the implicit use of Rspec `subject`. – cenouro Sep 24 '15 at 18:25
  • possible duplicate of [Set Rspec default GET request format to JSON](http://stackoverflow.com/questions/11022839/set-rspec-default-get-request-format-to-json) – Alexey Shein Sep 24 '15 at 18:30

1 Answers1

1

You don't need setting render_views for request specs. In fact, the error you're getting right now says that views are actually rendering, it's just there's no correct one for your request type (html).

You need to add format: :json to your second parameter in your request, like this:

@data[:format] = :json

post '/users/sign_in', @data
Alexey Shein
  • 7,342
  • 1
  • 25
  • 38