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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I have code such at this in a method:

Property1.Update()
Property2.Update()

I'm mocking something else in this method that occurs before these two calls, so for my test, I just want to fake those two Update calls out, but I don't want to right extra code in my test to totally fake the properties and swap them...

So ideally I want:

Isolate.WhenCalled(() => mock.ExecuteScaler()).WillReturn(-1);
Isolate.NonPublic.WhenCalled<Property1Type>("Update").Ignore();
Isolate.NonPublic.WhenCalled<Property2Type>("Update").Ignore();

Hope that makes sense...

Thanks
asked by boo (21.8k points)

4 Answers

0 votes
Brian,

If I understand correctly, you don't want to create a fake of the instance and then swap it, but rather specify that the method "update" will be ignored on all instances of a specific type.

We don't have the equivalent of the reflective MockAll in AAA. We plan to introduce it in the future. And your suggestion looks pretty interesting - we'll put it in our backlog :lol:

Thanks,
answered by gilz (14.5k points)
0 votes
Gil...came up with a work around - not sure if it would be considered 'good' but it actually made things quite simple and hardly in code (which is always good)

I actually faked the entire target class, and then used CallOriginal on the method I was to test against.

So I ended up with 3 fakes:
1-The target class (to fake private methods called from the method I'm testing.
2-SqlCommand (to mock ExecuteScaler() return)
3-SqlConnection (for performance only - needs to be a better seam, but legacy code!)
answered by boo (21.8k points)
0 votes
Brian,

Can you post your final code? It can be a good example for other users.

Thanks
answered by gilz (14.5k points)
0 votes
I'll see if I can summarize (real code is daunting legacy code):

//Arrange
Foo fake = Isolate.Fake.Instance<Foo>();
Isolate.Swap.NextInstance<Foo>().With(fake);

Isolate.WhenCalled(() => fake.MethodUnderTest()).CallOriginal();

// target actually ends up being a fake!
Foo target = FooFactory.GetFoo();

//remainder of any 'arrange' code
...

//Act (we're calling the *original* method!)
target.MethodUnderTest();

//Assert
...


The MethodUnderTest() method calls 'Update()' a number of times, but because it's fake (because the whole class is fake except the method I'm testing), I don't have to worry about things that happen within it or 'faking' necessary items in those other methods.

To be honest, now that I've discovered this, I'm using it fairly often - I'm primarily testing against fakes explicitly calling the original on the target method.

This works well for maintenance because now if someone adds or removes calls to other methods within my target method - the test doesn't have to be updated...think I'll have to blog that one - but more people read yours - so maybe better that you do...
answered by boo (21.8k points)
...