简体   繁体   English

使用类似Devise的路由时,如何在Rspec控制器测试中指定控制器和方法名称?

[英]how to specify controller and method names in Rspec controller test when using Devise-like route?

tl;dr: unable to rspec-test the devise controllers, help. tl; dr:无法rspec测试装置控制器,帮助。

I am using Devise and have the following routes (default devise_for routes): 我正在使用Devise并具有以下路由(默认的devise_for路由):

GET    /users/cancel(.:format)      devise/registrations#cancel
POST   /users(.:format)             devise/registrations#create
GET    /users/sign_up(.:format)     devise/registrations#new
GET    /users/edit(.:format)        devise/registrations#edit
PATCH  /users(.:format)             devise/registrations#update
PUT    /users(.:format)             devise/registrations#update
DELETE /users(.:format)             devise/registrations#destroy

I would like to test, using Rspec, that my controller method is spitting out the right output from a given set of parameters. 我想使用Rspec测试我的控制器方法是从给定的一组参数中吐出正确的输出。

My spec file looks like this: 我的规格文件如下所示:

describe Users::RegistrationsController, type: 'controller' do
  it 'creates a user with given parameters' do

    post 'create', username: 'jonsnow', password: '012345678912345', authenticity_token: '12345'
    expect(response.status).to eq(200)
  end

end

The error I get is: 我得到的错误是:

Failure/Error: post 'create', username: 'jonsnow', password: '012345678912345', authenticity_token: '12345'
ActionController::UrlGenerationError: 
No route matches {:action=>"create", :controller=>"users/registrations"}

It makes sense since I am using devise_for default routes and as you can see from the rake routes results, the correct route is devise/registrations#create , but I can't seem to find a way to specify the controller name to devise/registrations . 这是有道理的,因为我使用的devise_for缺省路由,你可以从看到rake routes的结果,正确的路线是devise/registrations#create ,但我似乎无法找到一个方法来指定控制器名devise/registrations I think it's 'inflected' from the controller class name or something to users/registrations . 我认为它是从控制器类名称“变形”到users/registrations And I would preferably like to avoid messing with my routes in order to make my test successful. 而且,为了使测试成功,我最好避免弄乱我的路线。 How can I accomplish that? 我该怎么做? (Please explain) Thanks in advance. (请解释)在此先感谢。

My gem versions: 我的宝石版本:

rspec '3.2.1'
ruby '2.1.5'
rails '4.2.0'

There is some additional configuration required to be able to test controllers that inherit from Devise. 为了测试从Devise继承的控制器,还需要一些其他配置。

describe YourControllerWhichInheritsDevise, type: 'controller' do
  before do
    @request.env["devise.mapping"] = Devise.mappings[:user]
  end

  it 'creates a user with given parameters' do
    post 'create', username: 'jonsnow', password: '012345678912345', authenticity_token: '12345'
    expect(response.status).to eq(200)
  end
end

From the Devise README : "If you are testing Devise internal controllers or a controller that inherits from Devise's, you need to tell Devise which mapping should be used before a request. This is necessary because Devise gets this information from the router, but since functional tests do not pass through the router, it needs to be stated explicitly." 从Devise自述文件中 :“如果要测试Devise内部控制器或从Devise继承的控制器,则需要在请求之前告诉Devise应该使用哪个映射。这是必要的,因为Devise从路由器获取了此信息,但是由于功能正常测试没有通过路由器,需要明确说明。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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