Rspec 配置
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
# before(:each) do
# @request.env["devise.mapping"] = Devise.mappings[:user]
# end
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
但是这个测试就没法通过,报错如下
1) 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)>'
各位有什么建议?