RhinoMocks AAA 语法
我花了一天的大部分时间试图弄清楚为什么简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
模拟不会开始返回记录的答案,直到使用
Replay()
切换到重播模式。存根和模拟的工作方式不同。我写了一篇关于差异的博客文章 。另请注意,您将旧的 record-replay-verify 语法与新的range-act-assert 语法混合在一起。对于 AAA,您不应使用模拟和
Expect
。相反,请像这样使用存根和 AssertWasCalled:请注意如何在开始时设置存根的行为与最后的断言是分开的。
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 andAssertWasCalled
like this: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.
方法
FileCopyDateCheck
是否真的使用精确字符串@"c:\Myprogram.txt"
和@"c:\ 调用Myprogram.txt"
作为参数?我不确定
FileInfo
是否正在使用c:\
执行某些操作。也许它被修改为大写的C:\
,这会让你的期望不起作用。也许尝试不检查确切参数值的期望
有关参数约束的更多详细信息,请参阅: 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 withc:\
. Maybe it is modified to upper caseC:\
, which would make your expectation not working.Maybe try an expectation which does not check for the exact argument values
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.
我认为这是因为您的 CopyvRAFiles() 方法不是虚拟的。
I think it's because your CopyvRAFiles() method isn't virtual.