RhinoMocks AAA语法 [英] RhinoMocks AAA Syntax
问题描述
我花了大部分时间试图弄清楚为什么简单的RhinoMocks测试不会返回我在返回值中设置的值.我确定我只是缺少一些非常简单的东西,但我无法弄清楚.这是我的测试:
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();
}
我为SystemIO类提供了一个接口,为此我将其模拟传递给了CopyFiles类.我在我的FileCopyDatCheck方法上设置了一个期望值,并说它应该返回("Test").当我单步执行代码时,它返回一个固定的null.有什么想法我在这里想念的吗?
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?
这是我的CopyFiles类方法:
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;
}
}
我认为"fileCopied"的返回值应由Expect设置. GetFilesForCopy返回myFileInfo中的两个文件.请帮忙. :)
I would think that "fileCopied" would have the Return value set by the Expect. The GetFilesForCopy returns the two files in myFileInfo. Please Help. :)
提前谢谢!
推荐答案
模拟将不会开始返回记录的答案,直到使用Replay()
将其切换到重播模式.存根和模拟不能以相同的方式工作.我已经写了关于差异的博客文章
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.
还要注意,您正在将旧的record-replay-verify语法与新的ranging-act-assert语法混合在一起.使用AAA时,不应使用模拟和Expect
.而是使用存根和AssertWasCalled
这样:
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"));
}
请注意,从一开始就设置存根的行为与末尾的断言是如何分开的. Stub
没有设定任何期望.
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.
这篇关于RhinoMocks AAA语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!