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

Sheshtawy #74

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Sheshtawy #74

wants to merge 4 commits into from

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
@@ -1,38 +1,57 @@
class NoApplesError < StandardError; end

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
end

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.

@@ -2,13 +2,93 @@
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

lib/tree.rb Outdated
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