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
In 6.0.1, this code throws an unexpected TypeMockException:

[ img ]
public class A
{
  public AFormatFileWriter FileWriter { get; private set; }

  public A()
  {
     FileWriter = new AFormatFileWriter();
  }

  public void Method()
  {
     FileWriter.WriteToFile();
  }

  public class AFormatFileWriter
  {
     public void WriteToFile()
     {
     }
  }
}

[TestMethod]
public void Test()
{
  Isolate.Swap.AllInstances<A>().With(
     Isolate.Fake.Instance<A>());
  A a = new A();
  //
  a.Method();
  //
  Isolate.Verify.WasCalledWithExactArguments(() => 
     a.FileWriter.WriteToFile());
}

Expected is Passed because a.FileWriter is a fake.
asked by Neil (27.7k points)

6 Answers

0 votes
Neil,

Does this work if you extract a.FileWriter to a variable before passing it to VerifyWasCalled?

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
No it still gives "Test method Tests.Test threw exception: TypeMock.TypeMockException:
*** Isolate.Verify does not support objects that were not faked using Isolate.Fake.Instance(), or passed through WhenCalled()."
answered by Neil (27.7k points)
0 votes
OK, my miss. What happened here is that A.Method() is faked because A is a recursive fake (it was swapped on creation). Because A is faked, A.FileWriter.WriteToFile() was not really called.

The problem is with the wrong exception message. This happens because of another feature related to future instance swapping - on swapping we copy the state of the real instance to the swapped instance, and in this case the swapped instance received the real instance of AFormatFileWriter. This causes a.FileWriter to return a real instance, which is not supported in Verify. I realize this requires a bit to understand, but you portrait a pretty tricky situation altogether - can you show how you would need to verify like that in a real test?

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi Doron,

My real test is just like I showed in the example code. Public method calls a private file-writing method which has been faked and I assert that that public method calls that private file-writing method.
answered by Neil (27.7k points)
0 votes
Hi Neil,

You can verify against the swapping instance:

[TestMethod]
public void Test()
{
    var swapper = Isolate.Fake.Instance<AAFormatFileWriter>();
    Isolate.Swap.AllInstances<AAFormatFileWriter>().With(swapper);
    A a = new A();
    // 
    a.Method();
    // 
    Isolate.Verify.WasCalledWithExactArguments(() => swapper.WriteToFile());
}


Please let me know if it helps.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Hi Elisha,

That way works, thanks.
answered by Neil (27.7k points)
...