Using Moq callbacks correctly according to AAA

Posted by Hadi Eskandari on Stack Overflow See other posts from Stack Overflow or by Hadi Eskandari
Published on 2010-05-06T07:23:25Z Indexed on 2010/05/06 7:28 UTC
Read the original article Hit count: 381

Filed under:
|
|

I've created a unit test that tests interactions on my ViewModel class in a Silverlight application. To be able to do this test, I'm mocking the service interface, injected to the ViewModel. I'm using Moq framework to do the mocking.

to be able to verify bounded object in the ViewModel is converted properly, I've used a callback:


[Test]
public void SaveProposal_Will_Map_Proposal_To_WebService_Parameter()
{
 var vm = CreateNewCampaignViewModel();
 var proposal = CreateNewProposal(1, "New Proposal");

 Services.Setup(x => x.SaveProposalAsync(It.IsAny<saveProposalParam>())).Callback((saveProposalParam p) =>
 {
  Assert.That(p.plainProposal, Is.Not.Null);
  Assert.That(p.plainProposal.POrderItem.orderItemId, Is.EqualTo(1));
  Assert.That(p.plainProposal.POrderItem.orderName, Is.EqualTo("New Proposal"));
 });

 proposal.State = ObjectStates.Added;
 vm.CurrentProposal = proposal;
 vm.Save();
}

It is working fine, but if you've noticed, using this mechanism the Assert and Act part of the unit test have switched their parts (Assert comes before Acting). Is there a better way to do this, while preserving correct AAA order?

© Stack Overflow or respective owner

Related posts about test

Related posts about unit-testing