Search code examples
ruby-on-rails-5rspec-rails

when code is incorrect should raise an error


I want see an error when code is incorrect but i see this failure

Failure/Error: raise AuthenticationError

 UserAuthenticator::AuthenticationError:
   UserAuthenticator::AuthenticationError

below codes from user_authenticator_spec

require '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

and below codes from user_authenticator

class UserAuthenticator
  class AuthenticationError < StandardError; end

  attr_reader :user
  def initialize(code)

  end

  def perform
    raise AuthenticationError
  end
end

Solution

  • I change this line code

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

    to

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

    and it's work!