mercredi 26 août 2015

What is the point of the lambda in VerifySet?

I'm a bit confused as to what the lambda in VerifySet of a moq object is supposed to do. As an example I have the following unit test:

var moqIMpmVisitorFacet = new Mock<IMpmVisitorFacet>();
moqIMpmVisitorFacet.SetupSet(s => s.TrackingSource=It.IsAny<TrackingSourceEnum>()).Verifiable();

//DoSomething sets TrackingSource to a value that should fail, i.e. XYZ
object.DoSomething(moqIMpmVisitorFacet.Object);

//does nothing?
moqIMpmVisitorFacet.VerifySet(s => s.TrackingSource = TrackingSourceEnum.MPM);

//fails assert?
Assert.AreEqual(TrackingSourceEnum.MPM, moqIMpmVisitorFacet.Object.TrackingSource);

So why specify a value in the lambda if it's not going to check that this value is set?

As it happens I can just use the Assert but the fact that this doesn't appear to be doing what I would of expected it to do is galling me.


I'm likely missing something fundamental here. This question has thrown me though, the answer states:

mockInvoice.VerifySet(x => x.InvoiceAttachmentId = 123, Times.Once());

Replace 123 with the expected value.

If you want to permit any value, use:

mockInvoice.VerifySet(x => x.InvoiceAttachmentId = It.IsAny<int>(),
    Times.Once());

why expect a value if it's just going to ignore it?

Aucun commentaire:

Enregistrer un commentaire