【问题标题】:How to unit test a method that returns Action<AuthenticationOptions>?如何对返回 Action<AuthenticationOptions> 的方法进行单元测试?
【发布时间】:2019-01-04 16:47:43
【问题描述】:

我正在尝试对这个返回 Action&lt;SomeOptions&gt; 的方法进行单元测试。

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" 的那部分代码

【问题讨论】:

  • 调用返回的动作并分配结果,然后验证结果。

标签: c# unit-testing asp.net-core


【解决方案1】:

正如@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);
}

【讨论】:

    猜你喜欢
    • 2011-06-26
    • 2015-06-08
    • 2014-05-10
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多