使用rspec测试设计自定义会话控制器

时间:2016-11-25 06:13:10

标签: ruby-on-rails rspec devise

我有自定义控制器

class Users::SessionsController < Devise::SessionsController
  # POST /resource/sign_in
  def create
    binding.pry
    super
  end
end

添加了路线

devise_for :users, controllers: { sessions: "users/sessions" }

它在使用浏览器登录时有效。但是内部控制器测试断点内部没有被击中:

RSpec.describe Users::SessionsController, type: :controller do

  describe 'POST #create' do

    context 'pending activation user with expired password' do
      it 'could not login' do
        user = create :operator_user, status: User.statuses[:activation_pending], password_changed_at: (1.day + 1.second).ago
        @request.env['devise.mapping'] = Devise.mappings[:user]
        sign_in user

        user.reload
        expect(user).to be_locked
      end
    end
  end

end

RSpec.configure do |config|
  #...
  # Devise methods
  config.include Devise::TestHelpers, type: :controller
  # ...
end

我期待表达 sign_in用户 落入我已经凌驾的创造方法。我做错了什么?

ps:它甚至属于标准设计SessionsController#create

1 个答案:

答案 0 :(得分:3)

您必须使用示例中的post :create, params: {...}而不是sign_in user

向控制器发送请求