mercredi 24 décembre 2014

Rails route is not found in the tests

This application is a Rails 4 app and just an API (at this point). I can hit my URL from the browser, but when I try to access it in a test it can't find the URL. I get this:



No route matches {:action=>"/api/v1/users/20", :controller=>"api/v1/users"}


There are not any assertions in my test yet. Just trying to get past this error first:



# /spec/controllers/api/v1/users_controller_spec.rb
require 'rails_helper'

RSpec.describe Api::V1::UsersController, :type => :controller do
describe "User API" do
it "can return a user by ID" do
user = FactoryGirl.create(:user)

get "/api/v1/users/#{user.id}"
end
end
end


And my controller:



# app/controllers/api/v1/users_controller.rb
class Api::V1::UsersController < ApplicationController
before_action :set_user, only: [:show]

def show
end

private

def set_user
@user = User.find(params[:id])
end
end


Any my routes:



# config/routes.rb
Rails.application.routes.draw do
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :users, only: [:show]
end
end
end


And rake routes give me:



Prefix Verb URI Pattern Controller#Action
api_v1_user GET /api/v1/users/:id(.:format) api/v1/users#show {:format=>"json"}


And my gems:



group :test do
gem 'capybara'
end

group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'database_cleaner'
end


I'm sure there is something simple I am missing here, but I've spent a couple hours and can't figure it out.


Aucun commentaire:

Enregistrer un commentaire