throw new MissingTextException();
One of the ideas of test first programming is that you shouldn't write code that unless you have a failing test. This can be a problem if you are writing code to make a test pass, when you find something new that you want to add, but you don't have a test for it. I don't like waiting until I am finished with the current test (I forget) and I don't like trying to write a new test in the middle of implementing another one. I understand that some people write stuff down, but I hate that, even pulling notepad for a to do is too distracting.
Let us say we have this code:
[Test] public void CanAddToDistributerIfHasEnoughSpace() { IDistributer distributer = new NavalDistributer() .SetNumberOfContainers(3); distributer.Add( new Container() ); Assert.AreEqual( 1, distributer.Containers.Count ); }
And the implementation is:
public class NavaDistributer : IDistributer { int numberOfContainers = 0; public ICollection<Container> Containers = new List<Container>(); public IDistributer SetNumberOfContainers(int numberOfContainers) { this.numberOfContainers = numberOfContainers; } public void Add(Container container) { if(Containers.Count >= numberOfContainers) throw new MissingTestException(); Container.Add(container); } }
The check for going over the allowed number of containers is simply shouting at me to build it. But I don't have a test for it, so I really shouldn't, I don't know what we should do in this case, since I haven't given it enough thought. Putting MissingTestException() (often with a descriptive message) keeps me in the flow of the test and satisfy the "you didn't handle this case" voice.
Comments
Not to nitpick or anything but I couldn't help but notice that HibernatingForums does not have any tests whatsoever.
Being true to tests first, you shoul not add the the check for number of containers at all if you don't yet have a test for it.
When writing tests and the implementation for the test, you write just as much as you need for the test to pass.
In your case I would split the test into two since it really tests two things
CanAddContainerToDistributer
and
[ExpectedException()]
WillThrowExceptionIfAddingMoreThenThereIsSpaceFor
Test design is often really hard. But one thing that is important is to try to test one thing in a single test.
I often use NotImplementedException for this case.
It doesn't, no.
There is a different focus there, but also because there isn't any functionality there yet.
I am not decided about whatever tests would be useful in the demonstration or not.
I asked myself the same question, yesterday. And I end up with the same answer that Patrik posted. I have still an unanswered question though.
Let's say I create a test for a function , then I make it pass. Good now I refactor (in this case : moving some code out the current function, and make a new function with it ). The new function isn't test yet! So, Ill do another test(s) for the new function !? That wouldn't be test first! So whats the good way to refactor and test the refactored code...??
Simon>That refactored code is still covered by tests so you dont need to write more tests.
Comment preview