Mocking Property Expectations with Moq

added by BlackWasp
10/19/2011 8:47:32 AM

156 Views

Moq provides similar capabilities when mocking properties of classes or interfaces as when mocking methods. Although the same verification and expectation set up methods can be used in some circumstances, there are additional options for properties.


6 comments

vijayst
10/19/2011 11:44:12 AM
Thanks for sharing a good example on how to use Moq with NUnit.

BlackWasp
10/19/2011 1:02:12 PM
You're welcome :)

There are another few mentions of Moq in my Unit Testing tutorial and I have some more planned too.

Noldorin
10/19/2011 7:16:27 PM
I've never really understood the purpose/role of mocking frameworks within tests. Surely if you want to "mock" something properly, you write the functionality yourself by implementing the desired interface?

BlackWasp
10/20/2011 4:24:51 AM
I use a mixture of manually written stubs and mocks, and mock objects generated by frameworks. I make the decision based upon what's easiest to maintain and the ease of development. In some cases the framework-based mocks can give good advantage over hand-rolled ones. If you haven't already, I would give it a try. It's another tool in the toolbox if nothing else.

I would never advise someone only use automatic mocks (or manual ones). In some situations frameworks add complexity and obscure the meaning of your code.

bradygaster
10/20/2011 6:07:42 AM
Excellent demonstration of a topic that confuses many developers. I love the approach to doing this. I'd prefer for it all to be on one single page, but that's a writer-preference thing. Great, easy-to-follow code examples, too, which is something many fail to provide when demonstrating mocking.

BlackWasp
10/20/2011 6:46:25 AM
Thanks :)