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
Hi

I do not know how to configure TypeMock to return different mocks for specific calls to constructors, i tried something like this:

var fakeSite = Isolate.Fake.Instance<SPSite>();
var fakeSite2 = Isolate.Fake.Instance<SPSite>();
Guid g = Guid.NewGuid();

Isolate.WhenCalled(() => new SPSite("specific string")).WithExactArguments().WillReturn(fakeSite);

Isolate.WhenCalled(() => new SPSite(g)).WithExactArguments().WillReturn(fakeSite2);

What am I doing wrong?
asked by damiand2 (2k points)

7 Answers

0 votes
Hi,

If you want to use specific contractor you should use the overload of Fake.Instance that let you specify the constructor arguments.
Also note that inside WhenCalled you can't use "new SPSite", instead you should use the fakes you created with Isolate.Fake.Instance

Example
Guid g = Guid.NewGuid();
var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes, ConstructorWillBe.Called, "specific string");
var fakeSite2 = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes, ConstructorWillBe.Called, g);

Isolate.WhenCalled(() => fakeSite.SomeMethod()).IgnoreCall();
// ...


Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
Hi
thanks for quick response, however provided Fake overload with Constructor.WillBeCalled will call ctor of SPSite, which is not what i want (in fact, it will break tests on build machine, where Sharepoint is not installed).

What I want is to return different fakes when different ctors are invoked (with specific, exact arguments), just like when using Isolate.WhenCalled().WithExactArguments().

Can i achieve this with TypeMock, without actually calling original ctors (as is currently with Constructor.WillBeCalled).

Method under test looks something like this:

void F1()
{
new SPSite(url1).OpenWeb()
new SPSite(url1).OpenWeb()
}

I want to test this method and must set up different expectations on each returned SPSite, based on ctor arguments (and calling original objects even partually is not an option)
answered by damiand2 (2k points)
0 votes
Correcting typo, of course strings passed to ctors are different:
void F1()
{
new SPSite(url1).OpenWeb()
new SPSite(url2).OpenWeb()
}

answered by damiand2 (2k points)
0 votes
Hi,

You can fake the SPSite instances based on the order that they are called.
This is not the best solution but until we'll implement the feature the workaround should work.

[Test, Isolated]
public void TestSP()
{  
   var fake1 = Isolate.Fake.Instance<SPSite>();
   var fake2 = Isolate.Fake.Instance<SPSite>();
   
   Isolate.WhenCalled(()=> fake1.OpenWeb()).WillReturn(fakeWeb1);
   Isolate.WhenCalled(()=> fake2.OpenWeb()).WillReturn(fakeWeb2);
   
   Isolate.Swap.NextInstance<SPSite>().With(fake1);
   Isolate.Swap.NextInstance<SPSite>().With(fake2);
   
     //call SomeClass.F1() ...
}
answered by ohad (35.4k points)
0 votes
Hi,

You can fake the SPSite instances based on the order that they are called.
This is not the best solution but until we'll implement the feature the workaround should work.


Ok, thank you for stright answer. Can you at least tell me estimated timewindow for that feature? I'd like to know if I can write such veeeeery fragile tests because in a month or two there will be fix or forget about it.
answered by damiand2 (2k points)
0 votes
Hi,

Sorry I don't know the time frame for the feature as we just released a new version this week.
However we are releasing new versions quit frequently and I hope we'll implement it in one of our upcoming versions.
answered by ohad (35.4k points)
0 votes
Sorry I don't know the time frame for the feature as we just released a new version this week.
However we are releasing new versions quit frequently and I hope we'll implement it in one of our upcoming versions.


Please let me know in this thread if you will know that some upcoming version of TypeMock supports returning different mocks based on ctor params.
answered by damiand2 (2k points)
...