MS Fakes:给定输入的垫片

时间:2016-03-10 03:51:30

标签: c# microsoft-fakes

假设我有一个方法

public void SomeFunction (int a) 
{
    return AnotherFunction (a + 1);
}

有没有办法用特定参数来填充AnotherFunction?我想的是:

using (ShimContext.Create())
{
    ShimMyClass.AnotherFunctionInt = (3) => 34; // return 34 if the given value is 3
    ShimMyClass.AnotherFunctionInt = (4) => 44; // return 44 if the given value is 4
    ShimMyClass.AnotherFunctionInt = (a) => 22; // default value
}

1 个答案:

答案 0 :(得分:1)

只需将代码添加到覆盖中,而不是返回硬编码值。像

这样的东西
    [TestMethod]
    public void SimpleTest()
    {
        TestUnitTestClass tutClass = new TestUnitTestClass();
        using (ShimsContext.Create())
        {
            ShimTestUnitTestClass shimClass = new ShimTestUnitTestClass(tutClass);
            shimClass.AnotherFunctionInt32 = (val) =>
            {
                if (val == 3)
                    return 34;

                if (val == 4)
                    return 44;

                return 22;
            };

            int curInt = tutClass.AnotherFunction(3);
            Console.WriteLine(curInt);
        }
    }