For example, for the coin changer kata, when the user enters anything less than 0, I want to return an empty array indicating I have no change for the user.
So my test looks like this:
it "returns nothing when amount is less than the smallest coin value" do CoinChanger.get_change(0).should == [] end
For a simplest case, the code looks like this:
get_change(amount) [] endIf you read the code, do you think it expresses my intention well?
What happen when the amount is 1 cent? The program changes to this:
def get_change(amount) if (amount == 0) [] else get_change(amount - 1) << 1 end end
But that's not what I mean. I really mean, if amount is 0, -1, or anything smaller than 0, I want it to return the empty array. So the code should really look like this:
def get_change(amount) if (amount <= 0) [] else get_change(amount - 1) << 1 end end
When I write the simplest test case, sometimes, I forget the other condition. In this case, maybe I need to add another test case to test what happen when amount is less than 0. In general, I should be more careful and understand why my test is passing.
Note: This is not the best way to write coin changer. I just want to demonstrate what I was thinking.
0 comments:
Post a Comment