如何对返回Action <authenticationoptions>的方法进行单元测试?

时间:2019-01-04 16:47:43

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

我正在尝试对该方法返回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"的那部分代码

1 个答案:

答案 0 :(得分:4)

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