Conversation
jaybobo
left a comment
There was a problem hiding this comment.
Looks good. I'd recommend reviewing better specs. It'll help with constructing readable, more easily maintained test.
| attr_#fill_in :height, :age, :apples, :alive | ||
|
|
||
| def initialize | ||
| attr_reader :height, :age, :apples |
There was a problem hiding this comment.
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 |
| class Apple < Fruit | ||
| attr_reader :color, :diameter | ||
|
|
||
| @@colors = ['red', 'yellow', 'green'] |
There was a problem hiding this comment.
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']
endrelatedly 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.
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.
|
|
||
| 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.
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.
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.
127..128 could also be written with subject or described_class
@jaybobo