When I tried to retrofit AutoFixture and auto-mocking on some existing tests, I ran into a stumbling block. This may indicate a code smell; my first inclination was to get it working, though.
A coworker (Brian) pointed me to this groundbreaking series of (short) videos. In them, Mark Seemann does a TDD kata using AutoFixture.
It took me a while to “get” this, and I had to write some code to try it out. But it’s a game-changer.
To get the full benefit of this, you have to be doing dependency injection (DI). Again, Mark Seemann comes to the rescue with his excellent book Dependency Injection in .NET.
When doing DI and TDD (test-driven development), it’s easy to spend a lot of time updating existing tests. You add a dependency, change a constructor, and have to change multiple test cases. There are ways around this (factories, Builder Pattern), but they take some effort.
Also, you can end up with a lot of constants that you don’t care about - test IDs, strings, etc. You need them - your mocks/fakes/doubles return them and your assertions verify the results against them - but you don’t care what they are.
Seemann’s techniques can eliminate most of this. The “arrange” portion of a test case (Arrange-Act-Assert) can shrink drastically.
I have not tried this on a production project yet, but I will be doing so. I want maintainable, easy-to-read unit tests.
The Moq mocking library only gets better. Here’s a LINQPad example of the functional specifications introduced in v4.
void Main()
{
var foo = Mock.Of<IFoo>(f =>
f.Id == 1 &&
f.Who == "me" &&
f.GetBar(It.IsAny<string>()) == Mock.Of<IBar>(
b => b.Name == "Fred"));
Console.WriteLine(foo.Id);
Console.WriteLine(foo.Who);
Console.WriteLine(foo.GetBar("whatever").Name);
// If you need verification and still want to use
// the functional style:
Mock.Get(foo).Verify(f => f.GetBar("whatever"));
}
public interface IFoo
{
int Id { get; }
string Who { get; }
IBar GetBar(string name);
}
public interface IBar
{
string Name { get; }
}
This prints:
1 me Fred
No more having to type "mock.Object"!
For more details, see Old style imperative mocks vs moq functional specifications and this Moq Discussions thread. Note that Daniel Cazzulino (kzu) suggests separating behavior-verification mocks and stubs; this syntax is most useful for stubbing.
I found WebSequenceDiagrams.com this week and was able to create the UML sequence diagram I wanted fairly quickly.
You enter text like
Alice->Bob: Authentication Request Bob-->Alice: Authentication Response
and it draws the diagram. You can change the style of the diagram to something more reasonable than “napkin.” ;) Diagrams can be saved as PNG images or PDF documents. (I’d recommend keeping a copy of the script text as well.)
The help page is quite good; you can also click on the examples on the left to insert the equivalent text. An area at the bottom shows script errors.
# Comments are allowed (but undocumented),
as are blank lines.
The site is free, but advanced features and local hosting are available for a fee.
There are other tools with similar capabilities. (Greg, I think you were the first to show me one - what was that?) There are a few techniques from UML Distilled I’d like to see, but this tool was good enough for my purpose.
Update - April 4, 2011
The one Greg mentioned is Quick Sequence Diagram Editor.
:: Next Page >>
Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.
Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.
| Next >