$customHeader
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

Using mocking in the test

The Unit Test

        [Test]
        public void load_all_of_file_using_delegate_with_mock()
        {
            // arrange
            string fileToLoad = "c:/tmp/KeyboardHandler.java.txt";
            FileLoader cut = new FileLoader();
            List<string> pretendFileContent = new();
            pretendFileContent.Add("Hello");
            pretendFileContent.Add("world");
            int expectedBytesRead = 10;
            var file = Substitute.For<MyFile>();
            file.ReadLines(fileToLoad).Returns(pretendFileContent);

            // act
            int bytesRead = cut.LoadFile(fileToLoad, (fname) =>
            {
                IEnumerable<string> result = file.ReadLines(fileToLoad);

                return result;
            });

            // assert
            Assert.AreEqual(expectedBytesRead, bytesRead);
        }
  • No labels