24x*_*mer 6 c# unit-testing rhino-mocks
我有一个方法,它恰好调用另一种方法4次,每次使用不同的参数.我想写了4个不同的单元测试用例来检查方法,每个调用都有一个特定的值.
以下是我的方法的外观:
public void MainMethod()
{
IServiceProvider serviceProvider = GetServiceProvider();
string value1 = GetValueFromStorage("SomeArg1");
// Call AnotherMethod
serviceProvider.AnotherMethod(value1);
string value2 = GetValueFromStorage("SomeArg2");
// Call AnotherMethod
serviceProvider.AnotherMethod(value2);
string value3 = GetValueFromStorage("SomeArg3");
// Call AnotherMethod
serviceProvider.AnotherMethod(value3);
string value4 = GetValueFromStorage("SomeArg4");
// Call AnotherMethod
serviceProvider.AnotherMethod(value4);
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试方法:
public void TestMainMethod()
{
// Stub storage
IDataStorage dataStorage = MockRepository.GenerateStub<IDataStorage>();
// Stub serviceProvider
IServiceProvider dataStorage =
MockRepository.GenerateStub<IServiceProvider>();
// stub for SomeArg1
dataStorage.Stub(x => x.GetValueFromStorage(null)
.IgnoreArguments().Return("Value1"))
.Repeat.Once();
// stub for SomeArg2
dataStorage.Stub(x => x.GetValueFromStorage(null)
.IgnoreArguments().Return("Value2"))
.Repeat.Once();
// stub for SomeArg3
dataStorage.Stub(x => x.GetValueFromStorage(null).IgnoreArguments()
.Return("Value3")).Repeat.Once();
// stub for SomeArg4
dataStorage.Stub(x => x.GetValueFromStorage(null).IgnoreArguments()
.Return("Value4")).Repeat.Once();
// call MainMethod
MainMethod();
// Assert that third call is called with "Value3"
serviceProvider.AssertWasCalled(x => x.AnotherMethod("Value3"));
}
Run Code Online (Sandbox Code Playgroud)
这似乎是我不能忽略其他调用,只是验证第三个调用是用一个特定的参数调用的(或者对于序列中的任何其他调用).似乎我必须四次调用"AssertWasCalled"并按顺序检查个别参数.那我怎么能做到这一点?或者我在这里遗漏了什么?
我认为你可以使用GetArgumentsForCallsMadeOn(Action<T>). 自从我使用它以来已经很长时间了,但它为您提供了一个包含对象数组的列表,这些对象是每次调用的调用参数。
| 归档时间: |
|
| 查看次数: |
1004 次 |
| 最近记录: |