Mocking Property Expectations with Moq(www.blackwasp.co.uk)

submitted by BlackWaspBlackWasp(4212) 7 months, 7 days ago

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.

11 comments |category: |Views: 26

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by vijaystvijayst(1311) 7 months, 7 days ago 0

Thanks for sharing a good example on how to use Moq with NUnit.

Reply

posted by BlackWaspBlackWasp(4212) 7 months, 7 days ago 0

You're welcome :)

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

Reply

posted by NoldorinNoldorin(829) 7 months, 7 days ago 0

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?

Reply

posted by BlackWaspBlackWasp(4212) replied to NoldorinNoldorin(829), 7 months, 6 days ago 0

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.

Reply

posted by NoldorinNoldorin(829) replied to BlackWaspBlackWasp(4212), 7 months, 6 days ago 0

My mocks never have any excessive code/boilerplate. I don't see what a mocking framework can possibly add, except lines of code! I generally don't try anything new unless I see a reason to be dissatisfied with my existing solution.

Still, people seem to use them, so I'm not going to say they shouldn't exist at all...

Reply

posted by BlackWaspBlackWasp(4212) replied to NoldorinNoldorin(829), 7 months, 5 days ago 0

All I can suggest is to give it a try. To me, the more tools in the toolbox the better. You don't have to use them all but knowing multiple tools gives you more options and can change the way you think about some problems.

Reply

posted by NoldorinNoldorin(829) replied to BlackWaspBlackWasp(4212), 7 months, 3 days ago 0

Fair suggestion. I can't say exactly what I'm missing (or not as it may be) until I give it a little go at least. :-) Will do so when I have sufficient time/opportunity. Cheers.

Reply

posted by BlackWaspBlackWasp(4212) replied to NoldorinNoldorin(829), 7 months, 2 days ago 0

Let me know how you get on.

Reply

posted by NoldorinNoldorin(829) replied to BlackWaspBlackWasp(4212), 7 months, 1 day ago 0

Will do. I forsee an upcoming project that will require a good deal of unit testing, so hopefully then. :-)

Reply

posted by bradygasterbradygaster(4897) 7 months, 6 days ago 0

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.

Reply

posted by BlackWaspBlackWasp(4212) replied to bradygasterbradygaster(4897), 7 months, 6 days ago 0

Thanks :)

Reply

information Login or create an account to comment on this story