5

I'm working with a web api in rails 4.1.8 and i'm doing some testing but always is returning me

Failure/Error: post '/v1/users', subdomain: 'api', user: @user.to_json, format: :json
 ActionController::UrlGenerationError:
   No route matches {:action=>"/v1/users", :controller=>"api/v1/users", :format=>:json, :subdomain=>"api", :user=>"JSON_OBJ"}

but if i test it in the browser with Advance Rest Console it works, i don't know what i'm doing wrong

here is me code

routes.rb

namespace :api, path: '/', constraints: {subdomain: 'api'}, defaults: { format: :json } do
  namespace :v1 do
    with_options except: [:edit, :new] do |except|
      except.resources :users do
        collection do
          post 'login'
          post 'showme'
        end
      end
      except.resources :products
      except.resources :locations
    end
  end
end

and my controller spec

module API
  module V1
    describe UsersController do
     before do
        request.host = "api.example.com"
        expect({:post => "http://#{request.host}/v1/users"}).to(
          route_to( controller: "api/v1/users",
                    action: "create",
                    subdomain: 'api',
                    format: :json
                  )
        ) # => PASS

        # token expectations
        @auth_token = allow(JWT::AuthToken).to(
          receive(:make_token).and_return("mysecretkey")
        )
        expect(JWT::AuthToken.make_token({}, 3600)).to eq("mysecretkey")
      end
      describe "Create User" do
        before(:each) do
          @user = FactoryGirl.attributes_for :user
        end

        it 'should return a token' do
          post '/v1/users', subdomain: 'api', user: @user.to_json, format: :json # Error
          response_body = JSON.parse(response.body, symbolize_names: true)
          expect(response_body['token']).to eql "mysecretkey"
        end
      end
    end
  end
end

rake routes

   login_api_v1_users POST   /v1/users/login(.:format)   api/v1/users#login {:format=>:json, :subdomain=>"api"}

   showme_api_v1_users POST   /v1/users/showme(.:format)  api/v1/users#showme {:format=>:json, :subdomain=>"api"}

   api_v1_users GET    /v1/users(.:format)         api/v1/users#index {:format=>:json, :subdomain=>"api"}

                POST   /v1/users(.:format)         api/v1/users#create {:format=>:json, :subdomain=>"api"}

    api_v1_user GET    /v1/users/:id(.:format)     api/v1/users#show {:format=>:json, :subdomain=>"api"}

                PATCH  /v1/users/:id(.:format)     api/v1/users#update {:format=>:json, :subdomain=>"api"}

                PUT    /v1/users/:id(.:format)     api/v1/users#update {:format=>:json, :subdomain=>"api"}

                DELETE /v1/users/:id(.:format)     api/v1/users#destroy {:format=>:json, :subdomain=>"api"}
2
  • what if you change post '/v1/users' to post api_v1_users? Commented Dec 7, 2014 at 16:45
  • the same error No route matches {:action=>"api_v1_users", :controller=>"api/v1/users", ...} neither post api_v1_users_url nor post api_v1_users_path Commented Dec 7, 2014 at 17:00

1 Answer 1

2

As described in http://api.rubyonrails.org/classes/ActionController/TestCase/Behavior.html which is referenced in https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs, the first argument to post in your RSpec example is the name of the controller method to be called (i.e. :create in your case), not the route to that method.

Sign up to request clarification or add additional context in comments.

1 Comment

you're welcome. see updated answer for RSpec documentation which references that page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.