[英]Mocking a method with Action<T>
我是Unit Testing的新手,很高兴知道我是否犯了错误或者没有朝着正确的方向前进。
情况如下:
我正在尝试测试一个方法( MethodUnderTest
),该方法调用另一个方法( MethodWithAction
),该方法将Action<T>
作为参数。 我想模拟MethodWithAction
,但是根据返回值测试逻辑。
这是结构:
interface IInterface
{
void MethodWithAction(Action<string> action);
}
class MyClass : IInterface
{
public void MethodWithAction(Action<string> action)
{
string sampleString = "Hello there";
action(sampleString);
}
}
class ClassUnderTest
{
public IInterface Obj = new MyClass();
public string MethodUnderTest()
{
string stringToBeTested = string.Empty;
Obj.MethodWithAction(str =>
{
if (str.Contains("."))
stringToBeTested = string.Empty;
else
stringToBeTested = str.Replace(" ", string.Empty);
});
return stringToBeTested;
}
}
我的测试方法是这样的:
[TestMethod]
[DataRow("Hello, World", "Hello,World")]
[DataRow("Hello, World.","")]
[DataRow("Hello", "Hello")]
public void MethodUnderTestReturnsCorrectString(string sampleString, string expected)
{
var mockObj = new Mock<IInterface>();
mockObj.Setup(m=>m.MethodWithAction(It.IsAny<Action<string>>))
.Callback(???);
ClassUnderTest sut = new ClassUnderTest();
sut.Obj=mockObj.Object;
string actual = sut.MethodUnderTest();
Assert.Equal(expected, actual);
}
我想知道在什么地方???
在测试中,还是有完全不同的方法来解决这个问题?
获取在回调中传递给mock的action参数,并使用示例字符串调用它。
mockObj
.Setup(m => m.MethodWithAction(It.IsAny<Action<string>>))
.Callback((Action<string> action) => action(sampleString));
参考Moq Quickstart以更好地理解如何使用此模拟框架。
我的第一直觉是重构ClassUnderTest
和IInterface
以便IInterface
有一个get属性,你可以完全删除IInterface实现的依赖,而MyClass只有一个工作要做(存储SampleString):
interface IInterface
{
string SampleString { get; }
}
// Fix MyClass
class MyClass : IInterface
{
public string SampleString => "Hello There"
}
class ClassUnderTest
{
public string MethodUnderTest(IInterface someObject)
{
string stringToBeTested = string.Empty;
if (someObject.SampleString.Contains("."))
stringToBeTested = string.Empty;
else
stringToBeTested = str.Replace(" ", string.Empty);
return stringToBeTested;
}
}
因此,我们可以完全删除Action,并且在测试时代码更易读,更容易理解。
只是看待问题的另一种方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.