我使用RSpec来测试我对Devise和 devise_for :users, controllers: {
registrations: 'users/devise/registrations',
sessions: 'users/devise/sessions'
}
gem混合物的实现。
我使用def create
req_from_coposition_app? ? respond_with_auth_token : super
end
def destroy
req_from_coposition_app? ? destroy_auth_token : super
end
告诉Devise在哪里查找我的会话控制器(source)。
it "should be able to sign in" do
request.headers["X-Secret-App-Key"] = "this-is-a-mobile-app"
request.env['devise.mapping'] = Devise.mappings[:user]
post :create,
user: {
email: user.email,
password: user.password
},
format: :json
expect(res_hash[:email]).to eq user.email
expect(res_hash[:authentication_token]).to eq user.authentication_token
end
在我的会话控制器中,如果请求具有特定标头(source),则会调用自定义方法。
it "should be able to sign out" do
token_before = user.authentication_token
request.env['devise.mapping'] = Devise.mappings[:user]
request.headers["X-Secret-App-Key"] = "this-is-a-mobile-app"
request.headers["X-User-Token"] = token_before
delete :destroy, nil, format: :json
expect(user.reload.authentication_token).to_not eq token_before
end
我有一个测试,确保用户可以获得身份验证令牌。 请求按预期工作(source)。
Users::Devise::SessionsController#destroy
接下来,我想测试退出(销毁身份验证密钥)。 我正在尝试使用以下代码:
response.status
然而,302
永远不会被击中。
response.body
#=> "<html><body>You are being <a href=\"http://test.host/\">redirected</a>.</body></html>"
为delete
,
post :create
我检查过rake路线,以不同方式格式化请求,并检查delete :destroy
方法是否与任何地方混淆。我完全不知道为什么BCResourceInterfaceChecker::isFresh() (called from bootstrap.php.cache at line 3061)
ResourceCheckerConfigCache::isFresh() (called from ResourceCheckerConfigCacheFactory.php at line 45)
ResourceCheckerConfigCacheFactory::cache() (called from classes.php at line 1328)
Router::getMatcher() (called from classes.php at line 1288)
Router::match() (called from classes.php at line 7170)
Router::match() (called from classes.php at line 2086)
RouterListener::onKernelRequest()
call_user_func() (called from WrappedListener.php at line 61)
WrappedListener::__invoke()
call_user_func() (called from classes.php at line 1853)
EventDispatcher::doDispatch() (called from classes.php at line 1771)
EventDispatcher::dispatch() (called from TraceableEventDispatcher.php at line 132)
TraceableEventDispatcher::dispatch() (called from bootstrap.php.cache at line 3178)
HttpKernel::handleRaw() (called from bootstrap.php.cache at line 3151)
HttpKernel::handle() (called from bootstrap.php.cache at line 3302)
ContainerAwareHttpKernel::handle() (called from bootstrap.php.cache at line 2498)
Kernel::handle() (called from app_dev.php at line 29)
有效,但composer.phar update symfony/symfony --with-dependencies
并没有达成行动。
答案 0 :(得分:1)
您不会在第4行https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb
上欺骗过滤器Devise认为没有人登录并且正在使你远离摧毁行动。跳过它,存根,欺骗它。