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!
Unfortunately, this works only for getter/setter properties and not for getter-only ones.
The reality of my projects is that most properties are getter-only 🙁
Hy Urs,
What you are saying is not true. Take a closer look at my example. Are you seing setter properties? You might have overlooked the equality operator. You need to use the equality operator and not the assignment operator. The parameter of Mock.Of is an Expression<Func>.
See https://bitbucket.org/danielmarbach/advancedtdd/src/tip/Sources/Solution/AdvancedTDD.Test/Models/ModelTest.cs and for example https://bitbucket.org/danielmarbach/advancedtdd/src/4e02b8313c33/Sources/Solution/AdvancedTDD/Models/IBook.cs
Have fun
@Daniel Marbach
Oops!
Your right. I’ll have to change some code tomorrow!
Happy mocking
Hi Daniel,
Thanks for this. I was having trouble finding any doco from Kzu on this, even the .CHM has no summary comments for Mock.Of. Can you point to any other sources on this very interesting feature please?
Hmmm… Interesting. Here it is:
http://www.google.com/codesearch/p?hl=de#ps_9V9W5zRA/trunk/Source/Linq/Mock.cs&q=Mock.Of%20package:http://moq\.googlecode\.com&d=3
Here I couldn’t find it… http://moq.codeplex.com/SourceControl/changeset/view/4d8f8b8f4d9a#src%2fSource%2fMock.cs
I’ll ping kzu.
Here quote from kzu:
@planetgeekch of course. it’s part of the public API now. If you need more than one, you do Mocks.Of instead.
Thanks Daniel. I quizzed @kzu a bit further and got this reponse:
@ArtOfMadness replace .Query with .Of and you’re good to go: http://kzu.to/hQhpYb
Hy
You can even clone my little advanced tdd project I once started:
http://bitbucket.org/danielmarbach/advancedtdd
Have fun
We’ve been using Moq for a while and didn’t realize you could do this, very cool. Could you show how to use a hierarchy like this in a test?
Thanks!
Hy Kevin
Sorry for the late response.
https://bitbucket.org/danielmarbach/advancedtdd/src/4e02b8313c33/Sources/Solution/AdvancedTDD.Test/Models/ModelTest.cs
Daniel