使用SystemWrapper和Rhino mock进行单元测试

本文关键字:单元测试 mock Rhino SystemWrapper 使用 | 更新日期: 2023-09-27 18:01:48

我有一个方法来备份一些文件:

 public void MakeBackup(IFileWrap file, string path)
    {
        if (path  == null)
            throw new ArgumentNullException();
        Console.WriteLine();
        string backups = Environment.CurrentDirectory + @"'Backups'";
        if (!Directory.Exists(backups))
            Directory.CreateDirectory(backups);
        if (file.Exists(path))
        {
            file.Copy(path,backups + Path.GetFileName(path),overwrite: true);
            Console.WriteLine("Backup of the " + Path.GetFileName(path) + " lies in the " + backups);
        }

}

我试图通过使用SystemWrapper和Rhino mock来测试它:

[TestMethod]
    public void MakeBackupTest()
    {
        IFileWrap fileRepository = MockRepository.GenerateMock<IFileWrap>();
        fileRepository.Expect(x => x.Exists(@"G:'1.txt"));
        fileRepository.Expect(x => x.Copy(@"G:'1.txt", Environment.CurrentDirectory + @"'Backups'1.txt", overwrite: true));
        new Windows().MakeBackup(fileRepository,@"G:'1.txt");

        fileRepository.VerifyAllExpectations();
    }

上面的测试失败。我做错了什么?

使用SystemWrapper和Rhino mock进行单元测试

您没有为fileRepository.Exists设置返回值-默认值将是false。它应该是这样的:

fileRepository.Expect(x => x.Exists(@"G:'1.txt")).Return(true);