NMock 3.5

September 23rd, 2007

One of the places where I am likely to actively use expression trees is testing/mocking.
(I am not the first one to notice this — I saw some post on expression tree asserts, but I lost it).

In my day-to-day development I am using NMock2.
Ayende‘s Rhino Mocks may be more convenient, but I do not like the syntax.
I just can’t read “expect call return” — my mind wants “expect call returns”.
Also, event support in Rhinos is really cryptic.
(I understand that it may be the only stringless way to express it).

So working on C# 3.0 project, I’ve looked into a stringless experience with NMock2.
What I wanted was to pass a method call expression, to change this:

Expect.Once.On(fs)
    .Method("GetDescendantDirectories")
    .With(RootPath)
    .Will(Return.Value(directories));

into this

Expect.Once
    .That(() => fs.GetDescendantDirectories(RootPath))
    .Will(Return.Value(directories));

Fortunately, it was quite easy to do with extension methods.
I have spent more time figuring better fluent syntax than actually writing it.

But the case where expression trees really shine, I think, is in the possibility to do this:

Expect.Exactly(directory.Files.Count)
    .That(() => access.IsPublicFile(Any.String))
    .Will(Return.Value(true));

I can analyze expression tree and find out that Any.String is not “a string”, but “any string”.
It is also easy to imagine Any.String.Matching(“^whatever”) and so on.
I have not tried to implement it, but I feel that it would be simple as well.

Also, while writing the post, I just got an idea — in the world of extenson methods we can do

Expect.Exactly(directory.Files.Count)
    .That(() => access.IsPublicFile(Any.String))
    .Will.Return(true);

without sacrificing extensibility.
“Will” can also be chained if more than one action is needed (however, a delegate would be better for this).

It would be nice to actually add this to NMock2 (as 3.5 branch), but they are using CVS, not SVN.
Also, am I the only one who thinks that SourceForge is slow and has hardly usable (bloated) design?

So if you want the code, you can ask me in comments, but it is primitive indeed.

  • Eyad.Salamin

    Wow! Great job Andrey, It seems like expression trees will bring back life to NMock2.
    Would you please send me copy of the code?

    Thanks.

  • Andrey Shchekin

    Thanks! Could you please give me your email?
    Mine is ashmind+blog(at)gmail.com.

  • http://codevanced.net/ Andrew

    Andrey,

    Nice one! I had a slightly different approach though (scroll downto “Improvements: NMock2 + C#3.” at http://codevanced.net/post/Touching-a-hot-iron-… )

  • http://blog.ashmind.com Andrey Shchekin

    Thanks! I really recommend looking at Moq, however, since NMock2 seems to be dead for a while now.
    I have moved to Moq and it saved me from writing a whole new API for NMock2 just to get more modern syntax.

    Also Moq people were very responsive at fixing bugs when they still had them.
    I wish I could say the same about latest NMock2.

  • http://codevanced.net/ Andrew

    >NMock2 seems to be dead for a while now.

    Latest release appears to be of December 06 2008 at sourceforge, and SVN statistics ( http://sourceforge.net/project/stats/detail.php… ) shows some checkins :)

    Why do you think it's “dead for a while”?

  • http://blog.ashmind.com Andrey Shchekin

    Now that's interesting.
    I am subscribed to their newsgroup and there was only occasional activity for almost a year (consistent with no releases for a while).
    It seems that they started releasing and the discussions moved elsewhere so that's the reason for my error.

    However unless they already support .NET 3.5 syntax, it still seems for me that MoQ is easier to use for NET 3.5 projects.

  • Urs Enzler

    Yes NMock2 is alive. But there is quite a bit room for confusion because you have to go to http://sourceforge.net/projects/nmock2/ and not nmock.org.
    The reason is that the developers of NMock version 1.0 and 2.0 (nmock.org) have stopped their development and it is continued by another team on http://sourceforge.net/projects/nmock2/

    Back to lambdas:
    Do you have a solution that supports defining expectations on properties and indexers as well?

    Happy mocking
    Urs

  • Urs Enzler

    Yes NMock2 is alive. But there is quite a bit room for confusion because you have to go to http://sourceforge.net/projects/nmock2/ and not nmock.org.
    The reason is that the developers of NMock version 1.0 and 2.0 (nmock.org) have stopped their development and it is continued by another team on http://sourceforge.net/projects/nmock2/

    Back to lambdas:
    Do you have a solution that supports defining expectations on properties and indexers as well?

    Happy mocking
    Urs

  • Urs Enzler

    Yes NMock2 is alive. But there is quite a bit room for confusion because you have to go to http://sourceforge.net/projects/nmock2/ and not nmock.org.
    The reason is that the developers of NMock version 1.0 and 2.0 (nmock.org) have stopped their development and it is continued by another team on http://sourceforge.net/projects/nmock2/

    Back to lambdas:
    Do you have a solution that supports defining expectations on properties and indexers as well?

    Happy mocking
    Urs

  • lbsa71

    You might be interested in my recent blog
    http://lbsa71.net/2010/08/18/stringless-nmock2-

  • Vikram30capri

    Hi Andrey,

    Really Great stuff. Can you share the code please. Email : vikram30capri@gmail.com