简体   繁体   English

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

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

I am trying to unit test this method which returns Action<SomeOptions> . 我试图对该返回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" 我想在测试中验证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"
}

I am not sure how to test that part of code that verifies that the result has Value1="abc" & Value2 = "def" 我不确定如何测试验证结果是否具有Value1="abc"Value2 = "def"那部分代码

As @Igor commented, you have to invoke the action and check the results of the action. 正如@Igor所说,您必须调用该动作并检查该动作的结果。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对返回 JsonResult 的 Action 方法进行单元测试? - How to unit test an Action method which returns JsonResult? 如何在返回IHttpActionResult时对web api动作方法进行单元测试? - How do I unit test web api action method when it returns IHttpActionResult? 如何在ASP.Net MVC中对包含对象数组并返回JsonResult的Action方法进行单元测试 - How to unit test an Action method that takes Array of an Object and returns JsonResult in ASP.Net MVC 如何对返回 Unit.Value 的 MediatR 方法进行单元测试 - How to unit test MediatR method that returns Unit.Value 如何对返回 ToList 的操作进行单元测试 - How do I unit test an action that returns a ToList 如何单元测试一个返回视图的GlassController动作模型 - How to Unit Test a GlassController Action which Returns a View Taking a Model 如何为返回十进制的 IActionResult 控制器操作创建单元测试? - How to create a unit test for an IActionResult controller action which returns decimal? 我如何单元测试返回PartialViewResult的MVC Action? - How can I unit test an MVC Action that returns a PartialViewResult? 单元测试返回void的方法 - Unit Test a method that returns a void 单元测试返回布尔值的方法 - Unit test a method that returns a boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM