-
Notifications
You must be signed in to change notification settings - Fork 74
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
Fixing the Tree #64
base: master
Are you sure you want to change the base?
Fixing the Tree #64
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I'd recommend reviewing better specs. It'll help with constructing readable, more easily maintained test.
|
||
def initialize | ||
attr_reader :height, :age, :apples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! not everything needs to be settable.
|
||
|
||
# Simple check if a tree has become too old. | ||
if @age >= 10 && age + @prng.rand(0..10) > 20 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice use of the getter here.
class Apple < Fruit | ||
attr_reader :color, :diameter | ||
|
||
@@colors = ['red', 'yellow', 'green'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class variables arent recommended.
instead you might use a constant or encapsulate them in a class method.
COLORS = %w(red yellow green).freeze
def self.colors
['red', 'yellow', 'green']
end
relatedly the cool thing about class << self
is that you can use it to organize your private class methods in the same way you would handle instance methods.
class << self
def foo
bar
end
private
def bar
:baz
end
end
12.times { tree.age! } | ||
end | ||
|
||
def new_test_tree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subject
is a better option depending on what you're trying to convey.
you could also use let
or let!` to accomplish this as well.
@@ -1,14 +1,137 @@ | |||
require 'rspec' | |||
require 'tree' | |||
|
|||
def kill_tree(tree) | |||
# The 12 here is based off of the rand_seed the below | |||
12.times { tree.age! } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this works but its preferred to either accomplish this with a helper method or let
. https://relishapp.com/rspec/rspec-rails/v/3-7/docs/helper-specs/helper-spec
|
||
describe '#any_apples?' do | ||
it 'should return true when there are apples' do | ||
tree = new_test_tree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the test tree
can be setup as a variable placed in an outer context.
describe Tree do
let(:tree) { described_class.new(...) }
describe "#kill" do
context "when tree is alive" do
it "kills tree"
tree.kill
expect(tree.dead?).to eq true
end
end
end
end
describe 'Fruit' do | ||
describe Fruit do | ||
it 'should have seeds when created' do | ||
f = Fruit.new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
127..128 could also be written with subject
or described_class
@jaybobo