Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATM Challenge: pry not recognizing Person #50

Open
pierre-1 opened this issue Feb 7, 2020 · 0 comments
Open

ATM Challenge: pry not recognizing Person #50

pierre-1 opened this issue Feb 7, 2020 · 0 comments

Comments

@pierre-1
Copy link

pierre-1 commented Feb 7, 2020

Problem

We are getting the following error in pry:

NameError: uninitialized constant Person

Background

All Rspec tests are passing but the program will not load.

Story

As a User,
I would like to create an account

Spec

Code in the person.rb file:

require './lib/person.rb'
require './lib/atm.rb'

describe Person do
    subject { described_class.new(name: 'Thomas') }

    it 'is expected to have a :name on initialize' do
        expect(subject.name).not_to be nil
    end

    it 'is expected to raise an error if no name is set' do
        expect { described_class.new }.to raise_error 'A name is required'
    end

    it 'is expected to have a :cash attribute with the value of 0 on initialize' do
        expect(subject.cash).to eq 0
    end

    it 'is expected to have a :account attribute' do
        expect(subject.account).to be nil
    end

    describe 'can create an Account' do
        before { subject.create_account }

        it 'of Account class ' do 
            expect(subject.account).to be_an_instance_of Account
        end

        it 'with himself as an owner' do 
            expect(subject.account.owner).to be_an_instance_of Person
        end

        it 'owner is the current subject' do 
            expect(subject.account.owner).to eq subject
        end
    end
    
    describe 'can manage funds if an account has been created' do
        let(:atm) { Atm.new }
        before { subject.create_account }
        it 'can deposit funds' do
            expect(subject.deposit(100)).to be_truthy
        end

        it 'funds are added to the account balance - deducted from cash' do
            subject.cash = 100
            subject.deposit(100)
            expect(subject.cash).to be 0
        end

        it 'can withdraw funds' do
            command = lambda { subject.withdraw(amount: 100, pin: subject.account.pin_code, account: subject.account, atm: atm) }
            expect(command.call).to be_truthy
        end

        it 'withdraw is expected to raise an error if no ATM is passed in' do
            command = lambda { subject.withdraw(amount: 100, pin: subject.account.pin_code, account: subject.account) }
            expect { command.call }.to raise_error 'An ATM is required'
        end

        it 'funds are added to cash - deducted from account balance' do
            subject.cash = 100
            subject.deposit(100)
            subject.withdraw(amount: 100, pin: subject.account.pin_code, account: subject.account, atm: atm)
            expect(subject.cash).to be 100
        end
    end


    describe 'cannot manage funds if no account has been created' do

        it 'cannot deposit funds' do
            expect { subject.deposit(100) }.to raise_error(RuntimeError, 'No account present')
        end
    end

end   

Implementation code

Here is our code for the account.rb file:

    attr_accessor :pin_code, :exp_date, :account_status, :balance, :owner

    STANDARD_VALIDITY_YRS = 5

    def initialize(attrs = {})
        @balance = 1000
        @account_status = :active
        @owner = set_owner(attrs[:owner])
        @pin_code = set_pin_code()
        @exp_date = set_expire_date()
    end

    def deactivate
        @account_status = :deactivated
    end

    private

    def set_expire_date
        Date.today.next_year(Account::STANDARD_VALIDITY_YRS).strftime('%m/%y')
    end

    def set_pin_code
        rand(1000..9999)
    end

    def set_owner(obj)
        obj == nil ? missing_owner : @owner = obj
    end

    def missing_owner
        raise "An Account owner is required"
    end
end

    ```

### Screenshots

```[1] pry(main)> load './lib/atm.rb'
=> true
[2] pry(main)> atm = Atm.new
=> #<Atm:0x00007fcfd2a2fe50 @funds=1000>
[3] pry(main)> person = Person.new(name: 'Thomas')
NameError: uninitialized constant Person
from (pry):3:in `__pry__'
[4] pry(main)> exit```

### How did you try to solve the problem?
Checked the person.rb file to identify issues. Cannot determine why we cannot create an account given that the Person class was initialized.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant