chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I am trying to use Isolator to mock a WCF service which is used by my class under test, instead of using the real service... and it's generally going really well :-)

The problem I have is that I pass the mocked service into the class under test (CategoryManager) and it is the CategoryManager that makes the call to the mocked service, rather than my test.

Then, when I call Isolate.Verify.WasCalledWithExactArguments() on the mocked service, it fails saying that the mock wasn't called with matching arguments. In fact, the mock asserts that it wasn't called at all by passing the test Isolate.Verify.WasNotCalled().

Let me show you some code:

First, I arrange the test/mock and pass the mock into CategoryManager:

var testService = Isolate.Fake.Instance<WCFServiceClient>();
var classUnderTest = new CategoryManager(testService);
Isolate.WhenCalled(() => testService.SaveCategory(testRequest)).WillReturn(expectedResponse);

Then my test call the CategoryManager:


Which, in turn, means the CategoryManager calls the mock service:


Then I try to verify the call was made, saying:

Isolate.Verify.WasCalledWithExactArguments(() => testService.SaveCategory(testRequest));

The only way the mock seems to detect having been called is if my actual test makes the call. When I mock the service, pass it to the CategoryManager, which then calls the mock (with all the correct arguments), it doesn't appear to register that fact it was called.

Is there anything obvious I'm missing here, or is this expected behaviour? I would expect the mock instance to know if it has been called or not, regardless of how the call may have been made.

If I call the mock service from with the test, using the same arguments, it works fine.

Any help greatly appreciated! :-)
asked by SidM (600 points)

1 Answer

0 votes

From what you posted I think the problem is that the argument to testService.SaveCategory in the test
Isolate.Verify.WasCalledWithExactArguments(() => testService.SaveCategory(testRequest));

is not the same argument you actually pass to the method when it runs.
I can't see where you pass it to the CategoryManager that in turns will pass it to WCFServiceClient.SaveCategory method.

I might be wrong here since I don't see the full test code.
If you try to use:
Isolate.Verify.WasCalledWithAnyArguments(() => testService.SaveCategory(testRequest));

does the test pass?

If I'm wrong please post the full test code I'm sure we can work it out :)
answered by ohad (35.4k points)