Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making actions get called in silverlight unit test with MOQ

Lets say I have this

_articlesService.SaveAsync(Model, AddressOf OnSaveCompleted)

The OnSaveCompleteMethod do a couple of things, obviously. Its a

Protected Overridable Sub OnSaveCompleted(ByVal asyncValidationResult As AsyncValidationResult)

In my unittest. I need to run a mocked SaveAsync, and have OnSaveCompleted called in anyway, because the method sends out events that I need to know have been sent.

Right now, the code just walks past that method, thus its never executed.

Need help solving this because I'm stuck right now.

like image 494
Einarsson Avatar asked Dec 09 '25 08:12

Einarsson


1 Answers

If I understand your context right:

  • you have a class under test which uses an ArticlesService
  • your ArticlesService (a collaborating class) is responsible for sending some events
  • you want to verify that your class under test is behaving correctly
  • you want to do that by checking for the events.

If that's the case, you may be making your class responsible for more than it needs to be. You only need to verify that the ArticlesService was asked to SaveAsync. You don't need to worry about what the ArticlesService then went off and did.

Think of it this way. You are a Class-Under-Test. You have too much work to do, so you've asked some other people to help you. You have two choices. You can either chase them up, worrying about whether they're doing it right, or you can just trust them.

Rather than micro-managing classes, you can write a separate test which gives some examples of the way the ArticlesService will work, which will check that the ArticlesService is doing its job correctly. Your CUT's responsibility is to delegate that work effectively.

If you actually need the events to be raised so that your CUT can respond, that's a separate aspect of its behaviour, and you can do it with Moq's "Raise" method, documented in "Events", here:

http://code.google.com/p/moq/wiki/QuickStart

Edit: You can also use "CallBack", documented on the same link, to do stuff with the args being passed to you, including OnSaveCompleted. Not sure if it's going to help or not; it's tricky to see what you're doing without both the code and the failing test. Good luck anyway!

like image 185
Lunivore Avatar answered Dec 12 '25 00:12

Lunivore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!