当我具有Assert.Null(result)和Assert.NotNull(result)时,xUnit测试如何通过?

时间:2018-08-28 16:33:35

标签: moq xunit autofixture

我将解决方案中的一些测试从MSTest重构为xUnit。即使我所有的测试都是使用MSTest通过的,但仍然进行了一些更改,使我想知道在重构期间所有事情如何这么容易通过。

我在测试结束时放入了矛盾的断言并进行了调试。即使结果看起来像我想要的样子(对象为null),它也传递了两个断言。这怎么可能?我在做什么错了?

以下是我的测试。命名已修改,因为这是我正在工作的代码,但是修改仅以简单的单词替换结束。我添加了注释以显示我的一些意图以及调试测试时发生的事情。

using AutoFixture.Xunit;
using Moq;
using My.Core.Project.Interfaces;
using My.Core.Project].Models;
using My.UI.Project.Factories.Sidebar;
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit.Extensions;

[Theory, AutoData]
public async Task GetReturnedObject_NoReturnedObject_EmptyResult(Guid returnedObjectIdentifier, ReturnedObjectType setupObject)
{
    //Arrange
    var mockServiceClient = new Mock<IServiceClient>(MockBehavior.Strict);

    var returnedObjectIdentifierParameter = returnedObjectIdentifier.ToString(); //Parse logic in the method exists, so I had to make sure the input was not just any string
    setupObject = null; //I want the search in the method to not find anything.

    mockServiceClient.Setup(c => c.GetReturnedObjectById(It.IsAny<Guid>())).ReturnsAsync(setupObject);

    var factory = new Factory();

    //Act
    var returnedObject = await factory.GetReturnedObject(mockServiceClient.Object, returnedObjectIdentifierParameter); //returnedObject is NULL.

    //Assert
    Assert.Null(returnedObject); //Hovered over returnedObject. It's NULL.
    Assert.NotNull(returnedObject); //Hovered over returnedObject, again. Still NULL. THIS SHOULD FAIL!
}

0 个答案:

没有答案