I have a controller with this index method (app/controllers/api/v1/users_controller.rb)
...
before_action :find_user, only: [:show, :destroy]
def index
@users = User.order('created_at DESC')
end
...
And I have a view (app/view/api/v1/users/index.json.jbuilder)
json.array! @users do |user|
json.id user.id
json.name user.name
json.posts user.posts do |post|
json.id post.id
json.title post.title
json.body post.body
end
end
And when I run the server it works fine, after accessing localhost:3000/api/v1/users
it is showing the expected output.
But when I launch these RSpec tests
(spec/controllers/api/v1/users_controller_spec.rb)
require 'rails_helper'
RSpec.describe Api::V1::UsersController, type: :controller do
describe "GET #index" do
before do
get :index
end
it "returns http success" do
expect(response).to have_http_status(:success)
end
end
end
I get an error
If I remove :index
from get :index
it gives the same error but (given 0, expected 1).
How come get :index
sending 2 parameters if there is only one and how can I rewrite this code so the test will pass?
If I rewrite index method like this
def index
@users = User.order('created_at DESC')
render json: @users, status: 200
end
The test will pass, but in this case I will not get the JSON file that I need (which I made with jbuilder)
curl
so that you can get a better stack trace? The error (anActionView::Template::Error
) and the work-around (don't use the template) suggest that your problem is in the jbuilder template. – mu is too short