简体   繁体   中英

no route matches error on route in rake routes

I'm testing a RESTful api in rails using rspec.

My request looks like this:

before(:each) do
    @user = FactoryGirl.create(:user)
    sign_in(@user)
end

it "returns a 200 code when a user checks a valid token" do
  get "/api/v1/users/#{@user.id}/token_check", token: @user.authentication_token
  expect(response.code).to eql(200)
end

when i run the testing suite, I receive the error:

Failure/Error: get "/api/v1/users/#{@user.id}/token_check", token: @user.authentication_token
     ActionController::UrlGenerationError:
       No route matches {:action=>"/api/v1/users/1/token_check", :controller=>"api/v1/users", :token=>"6fgswkHwWXrcyDQNJVBZ"}
     # ./spec/controllers/api/v1/users_controller_spec.rb:32:in `block (2 levels) in <top (required)>'

However, I can see the route for this action in rake routes:

 api_v1_user_token_check GET    /api/v1/users/:user_id/token_check(.:format) api/v1/users#token_check {:format=>:son}

I match this to my users_controller#token_check. Here is my controller and action:

def token_check
        render json: {
            result: ['Your authentication token is valid'],
        }, status: 200
    end

No route matches {:action=>"/api/v1/users/1/token_check", :controller=>"api/v1/users", :token=>"6fgswkHwWXrcyDQNJVBZ"}

As the error message shows, you just need to specify the action name.

from get "/api/v1/users/#{@user.id}/token_check", token: @user.authentication_token

to get "token_check", user_id: @user.id, token: @user.authentication_token

I was getting this same error. I solved this issue by simply moving it statement inside :type => :request describe statement like below:

   describe 'GET /v1/feeds/' , :type => :request do
      get '/api/v1/cars/random',  xhr: true,  headers: { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'  } 
        expect(response.status).to eq 401
      end
    end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM