简体   繁体   中英

How to unit test a method that returns Action<AuthenticationOptions>?

I am trying to unit test this method which returns Action<SomeOptions> .

public class MyOption
{
    public Action<SomeOptions> GetOptions()
    {
        return new Action<SomeOptions>(o =>
            {
                o.Value1 = "abc";
                o.Value2 = "def";
            }
        );
    } 
}

I would like to verify in my test that Value1 is "abc" and Value2 is "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"
}

I am not sure how to test that part of code that verifies that the result has Value1="abc" & Value2 = "def"

As @Igor commented, you have to invoke the action and check the results of the action. Try this:

[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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM