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 am working with the v. 6.0.70 of TypeMock.

I want to test the following:

public static IEnumerable<string> ReadLines (this StreamReader source)
        {
            string line;
            const string COMMENT = @"//";

            if (source == null)
                throw new ArgumentNullException (String.Format ("Source stream '{0}' argument was 'null'", source));
            // skipp comments
            while ((line = source.ReadLine ()) != null)
                if (!line.StartsWith (COMMENT))
                    yield return line;
        }


My problem is how do I for example iterate through a collection I have prepered in faking the file and returning one item at a one call.

I have set up a fakeReader and want to iterate over the fakeReader returning the item - just like
  [Test, Isolated]
        public void ReadLines_ReadAllLinesOfAFile_ReturnsEachLineButNoCommentLines ()
        {
            Stream fakeStream = Isolate.Fake.Instance<Stream> ();
            StreamReader fakeReader = Isolate.Fake.Instance<StreamReader> ();

            Isolate.WhenCalled (() => fakeReader.ReadLine ()). ?????? What to do?????

            List<string> lines = new List<string> ();

            foreach (var line in ReadLines (fakeReader))
                lines.Add (line);

            Assert.AreEqual (lines[0], _fileContent[0]);
            Assert.AreEqual (lines[1], _fileContent[3]);
        }


Could you give me a hint?

T'x
Peter
asked by mph (6k points)

2 Answers

0 votes
Hello,

From looking at your test code, it seems that you're trying to fake a StreamReader type - this is currently not possible with Isolator, since this type belongs to mscorlib, and we only currently have support for limited mscorlib types (DateTime.Now and File).

However there are a few things you can do instead of faking a stream reader, for instance, create a memory stream instead, that contains your test strings, and feed it to a real stream reader.

Alternatively, you can fake the ReadLines method completely, and have it return a list of your own, like this:

Isolate.WhenCalled(() => MyExtensions.ReadLines(null)).WillReturn(new List<string> { "line1", "line2", "line3..." });


As a side note, there's a hidden gotcha in your extension method - the guard clause against the null stream will not work - since you're using an iterator, the execution is deferred, and your null check will not be called until the foreach loop. Read Jon Skeet's post on the subject here.
answered by igal (5.7k points)
0 votes
Hi Igal,

thank you for the answer and the hint with MemoryStream.

The test was exactly targeted to check the behaviour of the extension methode. So Thank you for the comment on that one as well! :D
Peter
answered by mph (6k points)
...