Moq v4 functional specifications

04/20/11

Permalink 08:08:08 pm, by truewill Email , 165 words, 700 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.

Comments, Pingbacks:

No Comments/Pingbacks for this post yet...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
The name truewill is composed of two other words. What is the SECOND word?

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.

Search

Categories

Linkblog

b2evolution

contributors

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 3

powered by b2evolution free blog software