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

Unit Testing using SystemWrapper and Rhino Mocks

我有备份一些文件的方法:

 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 Mocks 对其进行测试:

[TestMethod]
    public void MakeBackupTest()
    {
        IFileWrap fileRepository = MockRepository.GenerateMock<IFileWrap>();

        fileRepository.Expect(x => x.Exists(@"G:.txt"));
        fileRepository.Expect(x => x.Copy(@"G:.txt", Environment.CurrentDirectory + @"\Backups.txt", overwrite: true));

        new Windows().MakeBackup(fileRepository,@"G:.txt");


        fileRepository.VerifyAllExpectations();

    }

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

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

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