MockOf: How neat is that?

Are you tired of using the object property in MOQ?

Are you tired of declaring a huge amount of local variables to declare complex hierarchies?

Have a look at the new Mock.Of<T> feature in MOQ! Let’s see an example.

    public interface ILibrary
    {
        string Name { get; }
        string Description { get; }
        IEnumerable<IBook> Books { get; }
        IEnumerable<ICategory> Categories { get; }
    }

    public interface IBook
    {
        string Name { get; }
        string ISBN { get; }
        string Description { get; }
        string Author { get; }
    }

    public interface ICategory
    {
        string Name { get; }
        IEnumerable<IBook> Books { get; }
    }

Use the new fluent api to configure your mock hierarchy…

            var library = Mock
                .Of<ILibrary>(l => l.Name == "TestLibrary"
                                   && l.Description == "Library used to demonstrate MOQ"
                                   && l.Books == new List<IBook>
                                                     {
                                                         Mock.Of<IBook>(b =>
                                                             b.Name == "The Wheel of Time Set I, Books 1-3"
                                                             && b.Description == "The Eye of the World/The Great Hunt/The Dragon Reborn"
                                                             && b.ISBN == "0812538366"
                                                             && b.Author == "Robert Jordan"),
                                                       Mock.Of<IBook>(b =>
                                                             b.Name == "The Wheel of Time Set II, Books 4-6"
                                                             && b.Description == "The Shadow Rising/The Fires of Heaven/Lord of Chaos."
                                                             && b.ISBN == "0812540115"
                                                             && b.Author == "Robert Jordan"),
                                                       Mock.Of<IBook>(b =>
                                                             b.Name == "The Complete Robot"
                                                             && b.Description == "complete collection of Robot stories from Isaac Asimov."
                                                             && b.ISBN == "0586057242"
                                                             && b.Author == "Isaac Asimov")
                                                     }
                                   && l.Categories == new List<ICategory>
                                                          {
                                                              Mock.Of<ICategory>(c =>
                                                                  c.Name == "Fantasy"
                                                                  && c.Books == l.Books.Where(x => x.Author.Equals("Robert Jordan"))),
                                                              Mock.Of<ICategory>(c =>
                                                                  c.Name == "Science Fiction"
                                                                  && c.Books == l.Books.Where(x => x.Author.Equals("Isaac Asimov")))
                                                          });

When printed to the console the hierarchy looks like:

TestLibrary
Library used to demonstrate MOQ

Books
    The Wheel of Time Set I, Books 1-3
    The Eye of the World/The Great Hunt/The Dragon Reborn
    Robert Jordan
    0812538366

    The Wheel of Time Set II, Books 4-6
    The Shadow Rising/The Fires of Heaven/Lord of Chaos.
    Robert Jordan
    0812540115

    The Complete Robot
    complete collection of Robot stories from Isaac Asimov.
    Isaac Asimov
    0586057242

Categories
    Fantasy
        The Wheel of Time Set I, Books 1-3
        The Eye of the World/The Great Hunt/The Dragon Reborn
        Robert Jordan
        0812538366

        The Wheel of Time Set II, Books 4-6
        The Shadow Rising/The Fires of Heaven/Lord of Chaos.
        Robert Jordan
        0812540115

    Science Fiction
        The Complete Robot
        complete collection of Robot stories from Isaac Asimov.
        Isaac Asimov
        0586057242

Isn’t that neat? Nice work, awesome work and many thanks to kzu! My daily developer life just got a bit more comfortable!

About the author

Daniel Marbach

10 comments

By Daniel Marbach

Recent Posts