Category: Tools

03/17/13

Permalink 10:34:12 am, by truewill Email , 38 words, 7429 views   English (US)
Categories: Tips, Testing, C#, IoC, .NET, Tools

Using Manual Fakes with AutoFixture

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.

Stack Overflow to the rescue!

03/14/13

Permalink 08:19:54 pm, by truewill Email , 206 words, 7851 views   English (US)
Categories: Testing, C#, IoC, .NET, Tools

A New Way of Testing

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.

04/20/11

Permalink 08:08:08 pm, by truewill Email , 165 words, 3641 views   English (US)
Categories: Tips, Testing, C#, IoC, .NET, Tools

Moq v4 functional specifications

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.

03/26/11

Permalink 10:23:11 am, by truewill Email , 179 words, 355 views   English (US)
Categories: Tools, Design

UML sequence diagrams

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.

03/24/11

Permalink 07:23:15 pm, by truewill Email , 49 words, 747 views   English (US)
Categories: C#, .NET, Tools, ReSharper

ILSpy

I did a (very) quick trial of the latest build (666!) of ILSpy the other day. I was impressed. Its features are very similar to those of .NET Reflector, and it correctly disassembled a fairly simple static method. It’s good to have alternatives. Hopefully ReSharper will have this ability soon.

:: Next Page >>

Development Central

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 >

Search

Categories

Linkblog

b2evolution

contributors

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 11

powered by b2evolution free blog software