小编24x*_*mer的帖子

RhinoMocks - 当一个方法被调用n次时,如何在n-k调用中测试它的参数

我有一个方法,它恰好调用另一种方法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)

c# unit-testing rhino-mocks

6
推荐指数
1
解决办法
1004
查看次数

标签 统计

c# ×1

rhino-mocks ×1

unit-testing ×1