Rspec config
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
...
路线
devise_for :users, controllers: { passwords: "users/passwords", sessions: "users/sessions", registrations: "users/registrations"}
关于SessionsController
class Users::SessionsController < Devise::SessionsController
end
测试代码
require 'spec_helper'
describe Users::SessionsController do
controller do
def after_sign_in_path_for(resource)
super resource
end
end
describe "After user sigin-in" do
before(:each) do
@user = FactoryGirl.create(:user)
end
it "change current_user" do
sign_in @user
expect( subject.current_user ).to eq(@user)
end
it "redirects to the user_root_path" do
user = FactoryGirl.create(:user)
controller.after_sign_in_path_for(@user).should == root_path
end
end
...
此测试成功通过!但是当我RegistrationsController
以同样的方式进行测试时,我收到了一个错误:
测试代码
require 'spec_helper'
describe Users::RegistrationsController do
controller do
def after_sign_up_path_for(resource)
super resource
end
end
describe "User sign_up" do
it "change current_user" do
post :create, user: FactoryGirl.attributes_for(:register_user)
expect( subject.current_user ).not_to be nil
end
...
错误
Users::RegistrationsController User sign_up change current_user
Failure/Error: post :create, user: FactoryGirl.attributes_for(:register_user)
AbstractController::ActionNotFound:
The action 'create' could not be found for AnonymousController
# ./spec/controllers/registrations_controller_spec.rb:18:in `block (3 levels) in <top (required)>'
答案 0 :(得分:1)
我在*_spec.rb
文件中对用户方法 controller 犯了一些错误,它定义了一个从所描述的类继承的匿名控制器。您也可以通过更改spec_helper.rb
config.infer_base_class_for_anonymous_controllers = false
您可以参考此链接以获取更多信息 https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller