Unit Testing File I/O

后端 未结 6 1760
故里飘歌
故里飘歌 2020-11-29 20:58

Reading through the existing unit testing related threads here on Stack Overflow, I couldn\'t find one with a clear answer about how to unit test file I/O operations. I have

6条回答
  •  有刺的猬
    2020-11-29 21:29

    Check out Tutorial to TDD using Rhino Mocks and SystemWrapper.

    SystemWrapper wraps many of System.IO classes including File, FileInfo, Directory, DirectoryInfo, ... . You can see the complete list.

    In this tutorial I'm showing how to do testing with MbUnit but it's exactly the same for NUnit.

    Your test is going to look something like this:

    [Test]
    public void When_try_to_create_directory_that_already_exists_return_false()
    {
        var directoryInfoStub = MockRepository.GenerateStub();
        directoryInfoStub.Stub(x => x.Exists).Return(true);
        Assert.AreEqual(false, new DirectoryInfoSample().TryToCreateDirectory(directoryInfoStub));
    
        directoryInfoStub.AssertWasNotCalled(x => x.Create());
    }
    

提交回复
热议问题