4

We are trying to use PostSharp, more specifically the OnMethodInvocationAspect, to intercept the methods of a class.

The code runs fine, but when testing it with MOQ, it seems to be messing up with my mocks.

If I remove the aspects, all tests succeed. But, if I turn the aspects back on, the expectations on the MOQ mocks are not met.

Here is a snippet taken from one of our unit tests:

this.sgtrMock.Setup(r => r.RetrieveCurrentTaxes()).Returns(new[] {tax1, tax2});
this.service.LoadServiceTaxes();
this.sgtrMock.Verify(r => r.RetrieveCurrentTaxes(), Times.Once());

Any ideas about what can be happening?

  • 1
    Can you post code of your aspect? There's a catch - Moq objects are not the intercepted objects, they're proxies. – Karel Frajták Sep 16 '11 at 13:39
  • Hey Karel, sorry about the long delay in responding... we had this problem quite a while ago and it is just a technical impracticability. Just like you said, MOQ creates proxies which end up circumventing PostSharp's aspects... We ended up refactoring the class to not include the aspect and moved it elsewhere (where we didn't need to to mock expectations). – Mozair Carmo Oct 29 '11 at 21:45
  • No problem, I posted my comment as an answer. Please, close this question with marking it, thanks. – Karel Frajták Oct 30 '11 at 08:32

1 Answers1

1

There's a catch - Moq objects are not the intercepted objects, they're proxies.

Karel Frajták
  • 4,389
  • 1
  • 23
  • 34