RhinoMocks AAA 语法

发布于 2024-08-23 08:53:45 字数 2197 浏览 2 评论 0原文

我花了一天的大部分时间试图弄清楚为什么简单的 RhinoMocks 测试没有返回我在返回中设置的值。我确信我只是错过了一些非常简单的东西,但我无法弄清楚。这是我的测试:

    [TestMethod]
    public void CopyvRAFiles_ShouldCallCopyvRAFiles_ShouldReturnTrue2()
    {
        FileInfo fi = new FileInfo(@"c:\Myprogram.txt");
        FileInfo[] myFileInfo = new FileInfo[2];
        myFileInfo[0] = fi;
        myFileInfo[1] = fi;
        var mockSystemIO = MockRepository.GenerateMock<ISystemIO>();
        mockSystemIO.Stub(x => x.GetFilesForCopy("c:")).Return(myFileInfo);
        mockSystemIO.Expect(y => y.FileCopyDateCheck(@"c:\Myprogram.txt", @"c:\Myprogram.txt")).Return("Test");
        CopyFiles copy = new CopyFiles(mockSystemIO);

        List<string> retValue = copy.CopyvRAFiles("c:", "c:", new AdminWindowViewModel(vRASharedData));
        mockSystemIO.VerifyAllExpectations();
    }

我有一个 SystemIO 类的接口,我将其模拟传递给我的 CopyFiles 类。我对 FileCopyDatCheck 方法设置了期望,并表示它应该返回(“Test”)。当我单步执行代码时,它返回一个 null 。我在这里缺少什么想法吗?

这是我的 CopyFiles 类方法:

    public List<string> CopyvRAFiles(string currentDirectoryPath, string destPath, AdminWindowViewModel adminWindowViewModel)
    {
        string fileCopied;
        List<string> filesCopied = new List<string>();
        try
        {
            sysIO.CreateDirectoryIfNotExist(destPath);

            FileInfo[] files = sysIO.GetFilesForCopy(currentDirectoryPath);

            if (files != null)
            {
                foreach (FileInfo file in files)
                {
                    fileCopied = sysIO.FileCopyDateCheck(file.FullName, destPath + file.Name);
                    filesCopied.Add(fileCopied);
                }
            }

            //adminWindowViewModel.CheckFilesThatRequireSystemUpdate(filesCopied);

            return filesCopied;
        }
        catch (Exception ex)
        {
            ExceptionPolicy.HandleException(ex, "vRAClientPolicy");
            Console.WriteLine("{0} Exception caught.", ex);

            ShowErrorMessageDialog(ex);
            return null;
        }
    }

我认为“fileCopied”将具有由 Expect 设置的返回值。 GetFilesForCopy 返回 myFileInfo 中的两个文件。请帮忙。 :)

提前致谢!

I've spent a good part of the day trying to figure out why a simple RhinoMocks test doesn't return the value I'm setting in the return. I'm sure that I'm just missing something really simple but I can't figure it out. Here's my test:

    [TestMethod]
    public void CopyvRAFiles_ShouldCallCopyvRAFiles_ShouldReturnTrue2()
    {
        FileInfo fi = new FileInfo(@"c:\Myprogram.txt");
        FileInfo[] myFileInfo = new FileInfo[2];
        myFileInfo[0] = fi;
        myFileInfo[1] = fi;
        var mockSystemIO = MockRepository.GenerateMock<ISystemIO>();
        mockSystemIO.Stub(x => x.GetFilesForCopy("c:")).Return(myFileInfo);
        mockSystemIO.Expect(y => y.FileCopyDateCheck(@"c:\Myprogram.txt", @"c:\Myprogram.txt")).Return("Test");
        CopyFiles copy = new CopyFiles(mockSystemIO);

        List<string> retValue = copy.CopyvRAFiles("c:", "c:", new AdminWindowViewModel(vRASharedData));
        mockSystemIO.VerifyAllExpectations();
    }

I have an interface for my SystemIO class I'm passing in a mock for that to my CopyFiles class. I'm setting an expectation on my FileCopyDatCheck method and saying that it should Return("Test"). When I step through the code, it returns a null insteaed. Any ideas what I'm missing here?

Here's my CopyFiles class Method:

    public List<string> CopyvRAFiles(string currentDirectoryPath, string destPath, AdminWindowViewModel adminWindowViewModel)
    {
        string fileCopied;
        List<string> filesCopied = new List<string>();
        try
        {
            sysIO.CreateDirectoryIfNotExist(destPath);

            FileInfo[] files = sysIO.GetFilesForCopy(currentDirectoryPath);

            if (files != null)
            {
                foreach (FileInfo file in files)
                {
                    fileCopied = sysIO.FileCopyDateCheck(file.FullName, destPath + file.Name);
                    filesCopied.Add(fileCopied);
                }
            }

            //adminWindowViewModel.CheckFilesThatRequireSystemUpdate(filesCopied);

            return filesCopied;
        }
        catch (Exception ex)
        {
            ExceptionPolicy.HandleException(ex, "vRAClientPolicy");
            Console.WriteLine("{0} Exception caught.", ex);

            ShowErrorMessageDialog(ex);
            return null;
        }
    }

I would think that "fileCopied" would have the Return value set by the Expect. The GetFilesForCopy returns the two files in myFileInfo. Please Help. :)

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-08-30 08:53:45

模拟不会开始返回记录的答案,直到使用 Replay() 切换到重播模式。存根和模拟的工作方式不同。我写了一篇关于差异的博客文章

另请注意,您将旧的 record-replay-verify 语法与新的range-act-assert 语法混合在一起。对于 AAA,您不应使用模拟和 Expect。相反,请像这样使用存根和 AssertWasCalled:

[TestMethod]
public void CopyvRAFiles_ShouldCallCopyvRAFiles_ShouldReturnTrue2()
{
    // arrange
    FileInfo fi = new FileInfo(@"c:\Myprogram.txt");
    FileInfo[] myFileInfo = new FileInfo[2];
    myFileInfo[0] = fi;
    myFileInfo[1] = fi;

    var stubSystemIO = MockRepository.GenerateStub<ISystemIO>();
    stubSystemIO.Stub(
        x => x.GetFilesForCopy(Arg<string>.Is.Anything)).Return(myFileInfo);
    stubSystemIO.Stub(
        y => y.FileCopyDateCheck(
            Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return("Test");

    CopyFiles copy = new CopyFiles(mockSystemIO);

    // act
    List<string> retValue = copy.CopyvRAFiles(
        "c:", "c:", new AdminWindowViewModel(vRASharedData));

    // make assertions here about return values, state of objects, stub usage
    stubSystemIO.AssertWasCalled(
        y => y.FileCopyDateCheck(@"c:\Myprogram.txt", @"c:\Myprogram.txt"));
}

请注意如何在开始时设置存根的行为与最后的断言是分开的。 Stub 没有设定任何期望。

分离行为和断言的优点是您可以在每个测试中进行更少的断言,从而更容易诊断测试失败的原因。

A mock will not start returning recorded answers until it is switched to replay mode with Replay(). Stubs and mocks do no work in the same way. I have written a blog post about the difference.

Also note that you are mixing the old record-replay-verify syntax with the new arrange-act-assert syntax. With AAA, you should not use mocks and Expect. Instead, use stubs and AssertWasCalled like this:

[TestMethod]
public void CopyvRAFiles_ShouldCallCopyvRAFiles_ShouldReturnTrue2()
{
    // arrange
    FileInfo fi = new FileInfo(@"c:\Myprogram.txt");
    FileInfo[] myFileInfo = new FileInfo[2];
    myFileInfo[0] = fi;
    myFileInfo[1] = fi;

    var stubSystemIO = MockRepository.GenerateStub<ISystemIO>();
    stubSystemIO.Stub(
        x => x.GetFilesForCopy(Arg<string>.Is.Anything)).Return(myFileInfo);
    stubSystemIO.Stub(
        y => y.FileCopyDateCheck(
            Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return("Test");

    CopyFiles copy = new CopyFiles(mockSystemIO);

    // act
    List<string> retValue = copy.CopyvRAFiles(
        "c:", "c:", new AdminWindowViewModel(vRASharedData));

    // make assertions here about return values, state of objects, stub usage
    stubSystemIO.AssertWasCalled(
        y => y.FileCopyDateCheck(@"c:\Myprogram.txt", @"c:\Myprogram.txt"));
}

Note how setting up the behavior of stubs at the start is separate from the assertions at the end. Stub does not set any expectations.

The advantage of seperating behavior and assertions is that you can make less assertions per test, making it easier to diagnose why a test failed.

戏剧牡丹亭 2024-08-30 08:53:45

方法 FileCopyDateCheck 是否真的使用精确字符串 @"c:\Myprogram.txt"@"c:\ 调用Myprogram.txt" 作为参数?

我不确定 FileInfo 是否正在使用 c:\ 执行某些操作。也许它被修改为大写的C:\,这会让你的期望不起作用。

也许尝试不检查确切参数值的期望

mockSystemIO.Expect(y => y.FileCopyDateCheck(Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return("Test");

有关参数约束的更多详细信息,请参阅: Rhino Mocks 3.5,参数约束

我非常确定也有可能使期望大小写不敏感。

Does the method FileCopyDateCheck really get called with the exact strings @"c:\Myprogram.txt" and @"c:\Myprogram.txt" as arguments?

I am not sure if FileInfo is doing something with c:\. Maybe it is modified to upper case C:\, which would make your expectation not working.

Maybe try an expectation which does not check for the exact argument values

mockSystemIO.Expect(y => y.FileCopyDateCheck(Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return("Test");

For more details about argument constraints see: Rhino Mocks 3.5, Argument Constraints

I am pretty sure that there are also possibilities to make the expectation case insensitive.

假情假意假温柔 2024-08-30 08:53:45

我认为这是因为您的 CopyvRAFiles() 方法不是虚拟的。

I think it's because your CopyvRAFiles() method isn't virtual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文