简体   繁体   English

RSpec测试设计了控制器操作,删除失败

[英]RSpec testing devise controller actions, DELETE failing

I'm using RSpec to test my implementation of the mixture of Devise and the simple_token_authentication gem. 我正在使用RSpec来测试Devise和simple_token_authentication gem的混合simple_token_authentication


I using devise_for to tell Devise where to look for my sessions controller ( source ). 我使用devise_for告诉Devise在哪里寻找会话控制器( )。

 devise_for :users, controllers: { 
    registrations: 'users/devise/registrations',
    sessions: 'users/devise/sessions'
  }

In my sessions controller, I invoke a custom method if the request has a specific header ( source ). 在我的会话控制器中,如果请求具有特定的标头( ),我将调用自定义方法。

def create
  req_from_coposition_app? ? respond_with_auth_token : super
end

def destroy
  req_from_coposition_app? ? destroy_auth_token : super
end

I have a test that ensures the user can get an auth token. 我进行了一项测试,以确保用户可以获取身份验证令牌。 The request works as expected ( 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

Next, I'd like to test signing out (destroying the auth key). 接下来,我要测试注销(销毁auth密钥)。 I am trying to use the following code: 我正在尝试使用以下代码:

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

However, the Users::Devise::SessionsController#destroy is never hit. 但是,永远不会命中Users::Devise::SessionsController#destroy response.status is 302 , and response.status302 ,并且

response.body
#=> "<html><body>You are being <a href=\"http://test.host/\">redirected</a>.</body></html>"

I have checked rake routes, formatted the request in different ways, and checked that the delete method wasn't being messed with anywhere. 我已经检查了rake路由,以不同的方式格式化了请求,并检查了delete方法在任何地方都没有混乱。 I am completely at loss as to why post :create works, but delete :destroy doesn't hit the action. 我完全不知道为什么post :create可以工作,但是delete :destroy没有起作用。

You're not fooling the before filter on line 4 here https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb 您没有在这里的第4行上欺骗过之前的过滤器https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb

Devise thinks no one is signed in and is bouncing you away from the destroy action. Devise认为没有人登录并且正在使您摆脱破坏行动。 Skip it, stub it, trick it. 跳过,存根,欺骗。

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

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