In the setup we created a solution with two projects. The purpose of these two projects was for a Sorting Kata. Now we write a sorting method to test.
We use CTRL+. to add the Bookshelf class to the BookSorting project. Then we use it again to add the LinqOrderBy method that accepts the list of books. Our goal is for the method to return a sorted list. Then we can assert that the correct book appears first.
Running the test causes the following error:
1 test failed: System.NotImplementedException
Hurray, the test failed. We use the Red, Green, Refactor pattern from Kent Beck's book, Test Driven Design By Example.
The problem is, it failed for the wrong reason. We want to see a failure because the books are not sorted. Let's take a look at the Bookshelf class.
Well, that explains our "NotImplementedException".
So, what is it going to take to get this test to fail for the right reason? Let's take a look at Uncle Bob's Transformation Priority Premise. The first transformation we should try is "nil". Can we get the test to fail by doing nothing? No, our code will not compile unless we return something. The next step is a constant. Let's just return the books list as is.
1 test passed
This is why we Red, Green, Refactor. Our test passed when we didn't want it to. Let's flip the order we put the books into the list.
1 test failed: Expected: <BookSorting.Book> But was: <BookSorting.Book>
Alright, it failed for the right reason. Now we need to deal with that failing test message. It's not very helpful. We have two options, change the Assert method to have a message, or overwrite the Book ToString method to return the name of the book instead of the object name "BookSorting.Book".
Here's an updated assert.
1 test failed Failed: Expected Partnoy, Frank. Wait: The Art and Science of Delay but was Watt, Andrew. Beginning Regular Expressions Expected: <BookSorting.Book> But was: <BookSorting.Book>
I would not want to write that much code for every assert. Plus, the expected message is still not helpful. Let's overwrite the Book ToString method.
We can roll back our assert.
1 test failed Expected: Partnoy, Frank. Wait: The Art and Science of Delay But was: Watt, Andrew. Beginning Regular Expressions
I can see the advantage of calling Book.ToString() and getting the author and title instead of <BookSorting.Book> so let's stick with that for now.
We saw a failing test (Red), and we made sure the message was helpful. Now we can make it green.
1 test passed
Please contact me about this Kata. I would love to get your feedback.
Let's continue the Kata by using the built-in sort method of a List.