MS Fakes:具有给定输入的垫片
MS Fakes: Shims with given input
假设我有一个方法
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
}
只需将代码添加到覆盖中,而不是返回硬编码值。像
[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);
}
}
假设我有一个方法
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
}
只需将代码添加到覆盖中,而不是返回硬编码值。像
[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);
}
}