我有一个方法,它恰好调用另一种方法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(); …Run Code Online (Sandbox Code Playgroud)