Skip to content

Conversation

@sheshtawy
Copy link

@jaybobo This test suite might not be covering all cases, but I wanted to get your feedback on how I'm writing tests vs best practices, and checking if I'm missing something major

Copy link
Member

@jaybobo jaybobo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. See comments.

lib/tree.rb Outdated

class Tree
attr_#fill_in :height, :age, :apples, :alive
attr_accessor :height, :age, :apples, :alive
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should all attributes be settable?

end

def age!
def age! # I'm not sure why this has a bang
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib/tree.rb Outdated
def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?
if self.any_apples?
return @apples.pop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require_relative '../lib/tree'

describe Tree do
let(:tree) { Tree.new(1, [], true, 1) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of .let. in rails apps, you'll often see use of a library like factory_bot for building factories to use in test. there are also fixture libraries that are popular.

https://github.com/thoughtbot/factory_bot

expect(tree.dead?).to be true
end

it 'is alive when calling dead? returns false' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great but i'd recommend using contexts here to have a clear happy vs unhappy path. it also improves readability. contexts can also be nested.

describe Tree do
  it 'has a particular attribute' do
    ...
  end
  
  context 'when alive' do
    it '..' do
    end
  end

  context 'when dead' do
    it '..' do
    end
  end
end

see: http://www.betterspecs.org/#contexts

if @age == 50
@alive = false
elsif
@age = @age + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@age += 1 is the shorthand equivalent.

BTW, I appreciate that it doesn't continue to age after it has died. 👍

lib/tree.rb Outdated
end

def any_apples?
@apples.length >= 1 ? true : false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ternary here is redundant. @apples.length >= 1 already returns true or false.

@apples.any? should do the trick, too.

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

Successfully merging this pull request may close these issues.

3 participants