Exemplo n.º 1
0
 public void ExampleOfATestSimulatingFileContainingMultipleLines()
 {
     string[] lines = ArraryContaining("Hello World", "Are you ok?");
     using (ClassUsingFile obj = new ClassUsingFile(StreamContaining(StringListBuiltFrom(lines))))
     {
         string[] actual = ArraryContaining(obj.GetStreamContentsAsLines());
         Assert.That(actual, Is.EqualTo(lines));
     }
 }
Exemplo n.º 2
0
        public void PreferPassingInMemoryStreamsRatherThanFilePath()
        {
            string s = "Hello World";

            using (ClassUsingFile unit = new ClassUsingFile(StreamContaining(s)))
            {
                Assert.That(unit.GetStreamContents(), Is.EqualTo(s));
            }
        }
Exemplo n.º 3
0
        public void ExampleOfHowToWriteTestThatUsesPhysicalFiles()
        {
            //strictly if a test touches the file system its not a unit test
            //(ie a unit test must be fast)!

            FileInfo file = TempFile.CreateWithContents("Hello");

            using (ClassUsingFile unit = new ClassUsingFile(file.FullName))
            {
                Assert.That(unit.GetStreamContents(), Is.EqualTo("Hello"));
            }
        }
Exemplo n.º 4
0
        public void PreferPassingInTextReaderRatherThanFilePath()
        {
            //if the Class Under Test (CUT) is expected to work against a file,
            //prefer passing in a TextReader/Writer. This allows our unit test to
            //simulate a file with just an in-memory string.
            //This assumes that the file being worked against is a text file.
            //If this is not true then prefer simulating a file using
            //MemoryStream and passing this to the CUT (see previous examples)

            string[] lines = ArraryContaining("Hello World", "Are you ok?");
            using (ClassUsingFile obj = new ClassUsingFile(ReaderContaining(StringListBuiltFrom(lines))))
            {
                string[] actual = ArraryContaining(obj.GetStreamContentsAsLines());
                Assert.That(actual, Is.EqualTo(lines));
            }
        }