0

I'm writing a test in Visual Basic .NET in Visual Studio 2012. In a particular Stub I'm sure all the arguments match but yet it only gets executed if I add IgnoreArguments to it, but since I need to have other similar stubs for that same method this is not acceptable for production.

I can't post a code sample (well, I could, but it would need to be really big in order to be useful). But it might suffice if I tell you that it's a request in the shape of:

Dim request As IMyRequest = MyService.CreateRequest(obj1, obj2, list1, 
    int1, int2, bool1, bool2, bool3, string1, bool4, int3)

And my Stub looks like:

_myService.Stub(Function(x) x.CreateRequest(obj1, obj2, list1, 
    int1, int2, bool1, bool2, bool3, string1, 
    bool4, int3)).IgnoreArguments.Return(myRequest)

And _myService is a mock defined in the TestInitialize method like so:

_myService= Mock.Get(Of IMyService)()

I've checked and all the values match. There's also another very similar test with a very similar call (which works perfectly without the IgnoreArguments clause) that I've used as sample, so I'm at a loss as to why this isn't working.

Q: I'm sorry to ask such a vague question, but can anyone think of anything I might be missing to check?

I'll clarify whatever doesn't make sense. Thanks for the read.

EDIT: whelp, it turned out (after yet another pair of eyes glanced at the code) that indeed the type of one of the parameters was off. yay for disgustingly similar class names I'm not sure how to close this, so just let it die. Thanks and sorry for robbing you of your time.

Sebastián Vansteenkiste
  • 2,234
  • 1
  • 19
  • 29
  • It's hard to say with the detail provided. Can you add more detail to provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Ryan Gates May 15 '14 at 13:07
  • I figured that might be the case, sorry. I'll look into it, though it's going to take me a while to anonymize the code since I can't post the real source here. Thanks. – Sebastián Vansteenkiste May 15 '14 at 13:12
  • Nope, I guess all I can do is ask for common advice for what to check for here. I've already asked some co-workers to look at the code and we all can't see the problem. So if anyone has had this problem in the past and can share his experience (even if that particular case doesn't apply in this scenario) it'll be gladly appreciated. – Sebastián Vansteenkiste May 15 '14 at 13:21

1 Answers1

1

I assume you are mocking the service here for your test. Object equality on the stub is probably what is causing it to not get called. Without more information it is hard to say but that is the first thing I would look at.

  • That was my first guess too, specially since the method being tested does quite a bit of work internally before calling this service. But the thing is, there's a very similar stub for a very similar service call on a very similar method on a very similar test that works perfectly without `IgnoreArguments`, so I don't understand why it doesn't work in this case. Thanks. – Sebastián Vansteenkiste May 15 '14 at 13:32
  • 1
    I was about to answer your other question as well. Use the syntax Arg.Is.Anything for the params that you want the stub to ignore (this might help you determine which one is causing the issue). In MoQ it is It.IsAny() and it would be used as such: MyService.CreateRequest(obj1, It.IsAny(), list1, int1, int2, bool1, bool2, bool3, string1, bool4, int3) – DigitalMoss May 15 '14 at 13:36
  • 1
    @SebastiánVansteenkiste DigitalMoss is right - that's the syntax you use to ignore specific parameters. Here is an answer to a related question that may be useful in getting acquainted with this syntax: http://stackoverflow.com/a/1946945/926713 – Lilshieste May 15 '14 at 13:44
  • Awesome, just a note: for vb.net, the sintax is: `Arg(Of String).Is.Anything`. Big thanks! (although, yes, my original question remains unanswered) – Sebastián Vansteenkiste May 15 '14 at 14:01