当代码不正确时应该引发错误

when code is incorrect should raise an error

我想在代码不正确时看到一个错误,但我看到了这个失败

Failure/Error:引发 AuthenticationError

 UserAuthenticator::AuthenticationError:
   UserAuthenticator::AuthenticationError

以下代码来自 user_authenticator_spec

需要'rails_helper'

describe UserAuthenticator do
  describe '#perform' do
    context 'when code is incorrect' do
      it 'should raise an error' do
        authenticator = described_class.new('sample_code')
        expect(authenticator.perform).to raise_error(AuthenticationError)
        expect(authenticator.user).to be_nil
      end
    end
  end
end

及以下来自 user_authenticator

的代码
class UserAuthenticator
  class AuthenticationError < StandardError; end

  attr_reader :user
  def initialize(code)

  end

  def perform
    raise AuthenticationError
  end
end

我改一下这行代码

expect(authenticator.perform).to raise_error(AuthenticationError)

expect{ authenticator.perform }.to raise_error(UserAuthenticator::AuthenticationError)

成功了!