Welcome to Typemock Q&A, where you can ask questions and receive answers from other members of the community.

StreamWriter.Write() fakable while StreamWriter.WriteLine() unfakable

0 votes

It is interesting in Isolator how various mscorlib types differ in whether or not they throw when faked:

Isolate.Fake.Instance<System.IO.Stream>(); // Does not throw
Isolate.Fake.Instance<System.IO.TextReader>(); // Does not throw
Isolate.Fake.Instance<System.IO.TextWriter>(); // Does not throw
Isolate.Fake.Instance<System.IO.StreamReader>(); // Does not throw
Isolate.Fake.Instance<System.IO.StreamWriter>(); // Does not throw
Isolate.Fake.Instance<System.Text.RegularExpressions.Match>(); // Does not throw
Isolate.Fake.Instance<System.Threading.Thread>(); // Throws
Isolate.Fake.Instance<System.Text.StringBuilder>(); // Throws
Isolate.Fake.Instance<System.IO.FileInfo>(); // Throws
Isolate.Fake.Instance<System.IO.BinaryReader>(); // Throws
Isolate.Fake.Instance<System.IO.BinaryWriter>(); // Throws
According to https://www.typemock.com/mscorlib-types , Isolator supports faking these mscorlib types: DateTime, Environment, File, Directory, FileStream, and X509Certificate.
For unlisted-yet-fakable type System.IO.StreamWriter, its behavior when faked is rather interesting in that the fake allows asserting that StreamWriter.Write() was called but disallows by way of NullReferenceException asserting that StreamWriter.WriteLine() was called:
Edit: Right click "Open image in new tab" in Chrome shows this screenshot in full size.
Code in screenshot as text:
[TestFixture, Isolated]
public class Tests
	public void TestFakabilityOfStreamWriterWrite()
		var fakeStreamWriter = Isolate.Fake.Instance<StreamWriter>(Members.MustBeSpecified);
		Isolate.WhenCalled(() => fakeStreamWriter.Write((string)null)).IgnoreCall();
		fakeStreamWriter.Write("Value"); // Does not throw
		Isolate.Verify.WasCalledWithExactArguments(() => fakeStreamWriter.Write("Value"));
		Assert.AreEqual(1, Isolate.Verify.GetTimesCalled(() => fakeStreamWriter.Write((string)null)));

	public void TestFakabilityOfStreamWriterWriteLine()
		var fakeStreamWriter = Isolate.Fake.Instance<StreamWriter>(Members.MustBeSpecified);
		Isolate.WhenCalled(() => fakeStreamWriter.WriteLine((string)null)).IgnoreCall();
		fakeStreamWriter.WriteLine("Value"); // Throws with NullReferenceException
		Isolate.Verify.WasCalledWithExactArguments(() => fakeStreamWriter.WriteLine("Value"));
		Assert.AreEqual(1, Isolate.Verify.GetTimesCalled(() => fakeStreamWriter.WriteLine((string)null)));



asked Jul 15, 2015 by NeilJustice (1,110 points)
edited Jul 15, 2015 by NeilJustice

1 Answer

0 votes

Thank you, Neil, for the information. As you've stated some of these mscorlib types are not fakable yet by Isolator.

We'll look into it.

- Raph

Typemock Support

answered Jul 19 by Raphy (810 points)