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 Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

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 , 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 14, 2015 by NeilJustice (81,570 points)
edited Jul 14, 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, 2017 by Raphy (3,890 points)