在ASP.Net Core中测试REST样式控制器

时间:2019-01-02 06:03:46

标签: c# asp.net-core xunit

我正在尝试对asp.net核心项目中的操作方法进行单元测试。当我期望测试成功时,测试将失败。似乎该问题是由于Action方法的返回类型引起的。

我还尝试了在BusinessLogic中测试返回类型为“ IEnumerable”的方法,该方法可以按预期运行。这是我正在尝试的代码。

控制器/操作方法:

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private IValueLogic _objValueLogic;
    public ValuesController(IValueLogic objValueLogic) {
        _objValueLogic = objValueLogic;
    }
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        IEnumerable<string> allValues = _objValueLogic.GetAll();
        return new ActionResult<IEnumerable<string>>(allValues);
    }
}

测试类:

public class ValueApiTest
{
    private ValuesController _objValuesController;
    public ValueApiTest() {
        Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
        mockValueLogic.Setup(x => x.GetAll()).Returns(new string[] {"Value1", "Value2"});
        _objValuesController = new ValuesController(mockValueLogic.Object);
    }
    [Fact]
    public void GetAll_Success() {
        ActionResult<IEnumerable<string>> expected = new ActionResult<IEnumerable<string>>(new string[] {"Value1", "Value2"});
        ActionResult<IEnumerable<string>> actual = _objValuesController.Get();
        Assert.Equal<ActionResult<IEnumerable<string>>>(expected, actual);
    }
}

我希望获得成功的结果,因为期望值和实际值都相等,但是失败并显示消息。如您所见,“期望值”和“实际值”打印相同。

Starting test execution, please wait...
[xUnit.net 00:00:03.01]     Test.WebApi.ValueApiTest.GetAll_Success [FAIL]
Failed   Test.WebApi.ValueApiTest.GetAll_Success
Error Message:
 Assert.Equal() Failure
Expected: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Actual:   ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Stack Trace:
   at Test.WebApi.ValueApiTest.GetAll_Success() in /home/saurabh/DevEnv/DotNetCore/dotnet-template/Test/Test.WebApi/ValueApiTest.cs:line 23

Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 4.5573 Seconds

1 个答案:

答案 0 :(得分:1)

您正在比较两种引用类型,如果两种引用类型引用的是同一对象,则它们将是相同的,在这种情况下,它们将不是相同的。您可以改用以下方法:

public class ValueApiTest
{
    private ValuesController _objValuesController;
    private string[] _expected = new string[] {"Value1", "Value2"};
    public ValueApiTest() {
        _expected = Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();          
        mockValueLogic.Setup(x => x.GetAll()).Returns(expected);
        _objValuesController = new ValuesController(mockValueLogic.Object);
    }
   [Fact]
    public void GetAll_Success() {
        IEnumerable<string> actual = _objValuesController.Get().Value;
        Assert.Equal<IEnumerable<string>>(expected, actual);
    }
}

当然,如果您的控制器返回ActionResult以外的其他对象,则此测试不会中断。如果您想解决这个问题,可以将其更改为

[Fact]
public void GetAll_Success() {
    ActionResult<IEnumerable<string>> result = _objValuesController.Get();
    IEnumerable<string> actual = result.Value;
    Assert.Equal<IEnumerable<string>>(expected, actual);
}

现在,如果控制器返回ActionResult<IEnumerable<string>>以外的任何内容,则测试将失败。