I think than you must to think about what you want to do and how you want to make it working. As example, you want to have a cat which say âmiaouâ. You can write a spec like this :
describe Cat
it "say miaou" do
expect(subject.speak).to eq "miaou"
end
end
Run it to make if fail. Do the simplest code to make it working. Refactor and pass to the next spec.
You want to have a cow which say âmeuhâ and you write this :
describe Cow
it "say miaou" do
expect(subject.speak).to eq "meuh"
end
end
Run it to make if fail. Do the simplest code to make it working. You donât really need to do a subclasses or a composition at this point. Imagine you want to introduce the notion of living
for both. First you implement it for Cat like this
describe Cat
it "say miaou" do
expect(subject.speak).to eq "miaou"
end
it "is living" do
expect(subject).to be_living
end
end
Run it to make if fail. Do the simplest code to make it working. Now, at the refactor part, you now you will use elsewhere so you can introduction a module and test it like this :
describe ActsAsAnimal
class Animal
include ActsAsAnimal
end
it "is living" do
expect(Animal.new).to be_living
end
end
Now, use the new module in your classes. Do you still need the spec in Cat spec? No because you assume that the module is working so you can remove it. You can now use the module in the cow class without to add a spec.
The final implementation can be like this :
class Cat
include ActsAsAnimal
def speak
"miaou"
end
end
class Cow
include ActsAsAnimal
def speak
"meuh"
end
end
module ActsAsAnimal
def living
true
end
end
Now lets talk about BDD insteed TDD. Does provide classes about Cow and Cat is what you want to give the final user? I think not. With BDD, you must to think about what the user want to do and how he what to do it. You must to start by the higher level with tools like capybara or cucumber. Run your spec to make it fail and when you make the code to make it work do the unit tests as in the example with Cow and Cat.
TDD and BDD are hard to learn. The important thing is to find your own way. How can you be more productive and how can you have a good feeling of what you are doing.
I did a longest answer than I except at the beggining. My answer is not the âtruthâ. Their is no âTrue TDDâ. There is only best practices provided by guys who went by the same way than you are doing now and say what they learn.
Bye!