c# - 如何对返回 Action 的方法进行单元测试?
问题描述
我正在尝试对这个返回的方法进行单元测试Action<SomeOptions>
。
public class MyOption
{
public Action<SomeOptions> GetOptions()
{
return new Action<SomeOptions>(o =>
{
o.Value1 = "abc";
o.Value2 = "def";
}
);
}
}
我想在我的测试中验证Value1
是"abc"
并且Value2
是"def"
[Test]
public void GetOptions_ReturnsExpectedOptions()
{
var option = new MyOption();
Action<SomeOptions> result = option.GetOptions();
//Assert
Assert.IsNotNull(result);
//I also want to verify that the result has Value1="abc" & Value2 = "def"
}
我不确定如何测试验证结果是否具有Value1="abc"
&的那部分代码Value2 = "def"
解决方案
正如@Igor 评论的那样,您必须调用该操作并检查该操作的结果。试试这个:
[Test]
public void GetOptions_ReturnsExpectedOptions()
{
var option = new MyOption();
Action<SomeOptions> result = option.GetOptions();
//Assert
Assert.IsNotNull(result);
//Assign SomeOptions and pass into the Action
var opts = new SomeOptions();
result(opts);
Assert.AreEqual("abc", opts.Value1);
Assert.AreEqual("def", opts.Value2);
}
推荐阅读
- qt - 如何在不重新制作所有目标的情况下使用 qmake 添加自定义构建步骤?
- flutter - 如何重新安装vs代码
- c# - Slack api 请求序列化
- cmake - CMake - if() 命令如何处理符号?作为字符串还是变量?
- uiswitch - “无效模式 'kCFRunLoopCommonModes' ...”是什么意思?
- python - 如何在扭曲的装饰器 defer.inlineCallbacks 中传递参数?
- java - 为什么Java中的RSA加密产生的密文长度与C#不同
- php - mysql无法连接:无效设置和其他错误
- python - Pandas SQL 或 Spark 中的部分旋转
- graphql - 对象数组的输入类型作为突变输入与乐观响应混淆