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



我正在尝试对返回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);
}

最新更新