没有路由匹配rake路由中的路由错误

时间:2015-12-08 03:46:23

标签: ruby-on-rails api rest rspec

我正在使用rspec在rails中测试RESTful api。

我的请求如下:

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

当我运行测试套件时,收到错误:

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)>'

但是,我可以在rake路线中看到此操作的路线:

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

我将它与我的users_controller#token_check相匹配。这是我的控制器和动作:

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

2 个答案:

答案 0 :(得分:2)

  

没有路由匹配{:action =&gt;“/ api / v1 / users / 1 / token_check”,:controller =&gt;“api / v1 / users”,:token =&gt;“6fgswkHwWXrcyDQNJVBZ”}

如错误消息所示,您只需指定操作名称。

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

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

答案 1 :(得分:0)

我遇到了同样的错误。我通过简单地将它在:type => :request的describe语句内移动如下语句来解决了这个问题:

   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
相关问题