Alexander Beletsky's development blog

My profession is engineering

New Tools in My TDD Arsenal

Recently my TDD arsenal has been enhanced with 3 new cool tools, which I’m about to share with you. More precisely it one tool and two frameworks. Let’s go for it.

NCrunch

NCrunch is just amazing extension for Visual Studio created by @remcomulder. It automatically detects all your tests and re-running those as soon as source code changes happen. Forget about manual test re-running, it’s just waste of time. You even do not need to press Ctrl + S, just continue coding as you usually do.

Initially I had big doubts about such kind of tools, but NCrunch changed my mind. It supports major unit test frameworks NUnit, XUnit, MSpec etc. Besides of that it allows to collect code coverage metrics (and show it just in VS editor), run tests under debugger, supports multi-core systems etc.

In short, NCruch is something that makes your TDD very smooth, allowing to focusing on important things and forgot about some routine.


ncrunch

NSubstitute

I stick to Moq for quite awhile and saw no reason to switch it.. Till I saw NSubsitute. I hardly could imagine someone who staring “yet another mocking framework project”, it looks like absolute non-sense.. But those guys proves me wrong.

Well, what’s so new there? First of all it have very clean API. No more new Mock() or MockGenerator.GenerateMock(), creation of test doubles are nothing more as Substitute.For<IEntityToMock>(). Mocking properties, multiple return values, events etc. in very easy fashion. Check out their getting started materials for further info.

Best feature as for me, that by using extension methods they got rid of lambdas for setting up mocks. It makes test code more readable and clean. See this small gist there I placed just some Moq and NSubstitue tests together.

I would not say that Rhino or Moq is now much worse that NSubstitute.. No, I would just say NSubstitute is a little better. Even same functionality, with less amount of code is already big argument for me.

[Test]
public void should_send_an_email_if_users_signs_up_nsub()
{ 
 // arrange
 var emailService = Substitute.For<IEMailService>();
 var controller = new LoginController(emailService);

 // act
 controller.SignUp(new SignUpModel { Email = "a@a.com", Password = "xxx" });

 // assert
 emailService.Received().SendEmail(Arg.Any<EmailMessage>(), "current");
}

FluentAssertions

Again, for years I followed classic NUnit’s Assert.That() method. I also played a bit with SharpTestsEx, but FluentAssertions by @ddoomen is going to change that.

FluentAssertions are based on extension methods and allows you to get rid of Assert.That call and just wrote your assertion directly to object. Here some example:

{
    // NUnit.Assert style..
    Assert.That(result, Is.EqualTo(3));

    // FluentAssert style..
    result.Should().Be(3);
}

This is very simple example. The power of FluentAssertions arise then you need to have either multiple assertions or assertions on complex objects. Multiple assertion could be combined by And, like:

{
    "somestring".Should().Contain("some").And.HaveLength(10);
}

It also provides great support for working with Collections, DateTimes, Guids, Exceptions, XML etc. Project is hosted on codeplex, here is documentation. Easy start, easy go.

Conclusions

Now, I’m sharpening my axe on those tools and have very nice impressions so far. Special thanks goes to @skalinets who introduced me with those tools.