【问题标题】:Unit Testing using SystemWrapper and Rhino Mocks使用 SystemWrapper 和 Rhino Mocks 进行单元测试
【发布时间】:2016-08-10 14:16:07
【问题描述】:

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

 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:\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();

    }

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

【问题讨论】:

  • 它是如何“失败”的?它是抛出异常,还是期望失败?请发布消息或例外。
  • 调试器是程序员最好的工具!

标签: c# unit-testing rhino-mocks systemwrapper


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2014-04-30
    相关资源
    最近更新 更多