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
The code below fails. I was expecting the reference to be swapped; however according to this old post: viewtopic.php?p=5268 reference swapping wasn't available in 2009, but they where considering it for future release. Is there a way to swap references? I'm on version 7.2.1.0

public class TestType
{
public string Name { get; set; }
}

[Test, Isolated]
public void Test()
{
var t1 = Isolate.Fake.Instance<TestType>();
Isolate.WhenCalled(()=>t1.Name).WillReturn("somevalue");
Isolate.Swap.NextInstance<TestType>().With(t1);

var t2 = new TestType();

Assert.AreEqual("somevalue",t2.Name);
Assert.AreEqual(t2.Name,t1.Name);
Assert.AreEqual(t1,t1);
Assert.AreEqual(t2, t2);
Assert.AreEqual(t1, t2); //why does this line fail. Shouldn't they be the same object.
}

Thanks Much,

MB
asked by MBeckius (4.9k points)

3 Answers

0 votes
Hi,

Assert.AreEqual(t1, t2);
fails because it is not the same object.

What happens under the hood is redirection of the calls from the real object's methods to the fake ones's.

Do you have a case where this is not enough and you need to swap the actual instance ?
answered by alex (18k points)
0 votes
Hello,

>>Do you have a case where this is not enough and you need to swap the actual instance ?

In this case I am trying to verify that the MVC model created in my controller action and returned via the ViewResult.Model property is the model I intended. So I am faking and swapping the next instance of the model and then trying to verify that it is equal to the ViewResult.Model property. It does not need to check any values of the returned model only that it is the instance I was expecting.

Thanks,

MB
answered by MBeckius (4.9k points)
0 votes
Hi,

Isolator doesn't provide with the ability to swap the ref's of the object.
The closest solution for your issue is to compare the state of your object with what you expect it to be.

Hope it helps.
answered by alex (18k points)
...