0

我有一个这样的控制器

class RegistrationsController < Devise::RegistrationsController
  before_filter :authenticate_user!, :only => [ :finalize ]
  prepend_before_filter :require_no_authentication, :only => [ :complete ]

  def complete
    #blah
  end

  def finalize
    #blah
  end
end

在路线中它看起来像这样:

Blah::Application.routes.draw do
  root to: "requests#index"

  devise_for :users, :path => '', :path_names  => { :sign_in => 'login', :sign_out => 'logout' }, :controllers => { :passwords => "passwords", confirmations: 'confirmations' }

  #blahblah

  devise_scope :user do
    get '/register/:id/:token' => 'registrations#new', as: 'register'
    put '/register/:id/:token' => 'registrations#complete', as: 'complete_user'
    match "/finalize" => 'registrations#finalize', :as => :finalize
  end

end

现在我想complete从测试中访问这个动作:

let(:params) {
  {
    id: 1, 
    invitation_token: "INVITATION_TOKEN", 
    email: 'email@reporter.com', 
    name: 'Name', 
    surname: 'Surname', 
    title: 'Title', 
    password: 'asdasdasd'
  }
}

it 'should be redirected to the page with information about sending confirmation email' do
  put :complete, params

  response.should redirect_to(verification_path)
end

它失败了:

  1) RegistrationsController#complete when invited email isn't on a whitelist should be redirected to the page with information about sending confirmation email
     Failure/Error: put :complete, params
     ActionController::RoutingError:
       No route matches {:id=>"1", :invitation_token=>"INVITATION_TOKEN", :email=>"email@reporter.com", :name=>"Name", :surname=>"Surname", :title=>"Title", :password=>"asdasdasd", :controller=>"registrations", :action=>"complete"}
     # ./spec/controllers/registrations_controller_spec.rb:29:in `block (4 levels) in <top (required)>'

如何complete从 RSpec 测试访问此操作?

4

1 回答 1

1

您的路线指定:

'/register/:id/:token'

但是,你的参数没有:token,它有:invitation_token

如果您将参数哈希更改为:token,它将起作用。


请注意,您可能还需要添加以下内容,但这只会在您克服路由错误后发生。

before do
  @request.env["devise.mapping"] = Devise.mappings[:user]
end
于 2013-06-03T15:26:05.343 回答