Ruby on rails Rspec测试API版本控制
我正在使用Rails4.1.8中的web api,我正在做一些测试,但总是返回给我Ruby on rails Rspec测试API版本控制,ruby-on-rails,ruby,ruby-on-rails-4,rspec,rspec-rails,Ruby On Rails,Ruby,Ruby On Rails 4,Rspec,Rspec Rails,我正在使用Rails4.1.8中的web api,我正在做一些测试,但总是返回给我 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", :form
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"}
但是如果我用AdvanceREST控制台在浏览器中测试它,我不知道我做错了什么
这是我的密码
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
还有我的控制器规格
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路由
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"}
如中所述,在RSpec示例中,
post
的第一个参数是要调用的控制器方法的名称(即在您的示例中,:create
),不是这种方法的路线。如果您将post'/v1/users'
更改为post-api\u-v1\u-users
?同样的错误没有路线匹配{:action=>“api\u-v1\u-users”,:controller=>“api/v1/users”,…}
既不是post-api\u-v1\u-users\u-url
也不是post-api\u-users\u-users\u-code>不欢迎您。请参阅参考该页面的RSpec文档的更新答案。