assert.isNotNull在作为范围的一部分填充的单元测试上失败

时间:2016-06-16 20:49:03

标签: c# .net unit-testing

我是单元测试的新手,我正在尝试编写一个测试,调用控制器根据GUID获取操作:

[TestClass]
public class DocumentControllerTests : TestBase
{
    [TestMethod, TestCategory(Unit)]
    public void DocumentController_Get_Returns_Ok()
    {
        //
        // arrange
        //
        var scope = new DefaultScope();
        var expected = Builder<Dom.Document>
            .CreateListOfSize(1)
            .Build();
        scope.DocumentProviderMock
            .Setup(x => x.GetAllAsync())
            .ReturnsAsync(expected);

        //
        // act & assert
        //
        var testId = scope.TestId;
        var actual = scope.InstanceUnderTest.Get(testId)
            .AssertOk<List<Api.Document>>()
            .Content;
        expected.ShouldBeEquivalentTo(actual, opt => opt.ExcludingMissingMembers());
    }

这是定义的范围类:

    private class DefaultScope : TestScope<DocumentController>
    {
        /// <summary>
        ///     initializes a new instance of the
        ///     Deloitte.DCIPP.Tests.Web.Service.DocumentStore.DocumentControllerTests.DefaultScope class.
        /// </summary>
        public DefaultScope()
        {
            LoggingProviderMock = new Mock<ILoggingProvider>();
            DocumentProviderMock = new Mock<IDocumentProvider>();
            DependencyResolverMock =
                MockDependencyResolverFor<IResourceUrlBuilder, ResourceUrlBuilder>(new ResourceUrlBuilder());
            TestId = Guid.NewGuid();

            TestWebDocument = Builder<Api.Document>
                .CreateNew()
                .Build();

            TestDocument = BuildDomainDocument(TestId);


            // use standard maps
            AutoMapperConfig.RegisterMaps(Mapper.Configuration, DependencyResolverMock);

            InstanceUnderTest = new DocumentController(DocumentProviderMock.Object, LoggingProviderMock.Object,
                Mapper.Engine);

            BootstrapControllerContext(InstanceUnderTest);
        }

        /// <summary>
        ///     Gets or sets the dependency resolver mock.
        /// </summary>
        /// <value>The dependency resolver mock.</value>
        public IDependencyResolver DependencyResolverMock { get; }

        /// <summary>
        ///     Log Category all messages will be associated with.
        /// </summary>
        /// <value>The expected log categories.</value>
        public LogCategories ExpectedLogCategories { get; } = LogCategories.DocumentStoreService;

        /// <summary>
        ///     Gets the logging provider mock.
        /// </summary>
        /// <value>The logging provider mock.</value>
        public Mock<ILoggingProvider> LoggingProviderMock { get; }

        /// <summary>
        ///     Gets the Document provider mock.
        /// </summary>
        /// <value>The Document provider mock.</value>
        public Mock<IDocumentProvider> DocumentProviderMock { get; }

        /// <summary>
        ///     Gets the identifier of the test.
        /// </summary>
        /// <value>The identifier of the test.</value>
        public Guid TestId { get; }

        /// <summary>
        ///     Gets or sets the test Document.
        /// </summary>
        /// <value>The test Document.</value>
        public Dom.Document TestDocument { get; set; }

        public Api.Document TestWebDocument { get; }

        public Dom.Document BuildDomainDocument(Guid expectedId)
        {
            // Invoke GetBytes method.
            var array = Encoding.ASCII.GetBytes("Byte Controller Test");

            return Builder<Dom.Document>
                .CreateNew()
                .With(x => x.Id, expectedId)
                .With(t => t.Data = array)
                .Build();
        }
    }

当我运行测试时,我在线上获得例外:

            .AssertOk<List<Api.Document>>()

这个方法是从另一个类调用的,但它相对简单。我得到的错误是assert.isNotNull失败:

enter image description here

我不明白为什么它是空的,因为我手头填充了范围。

我做错了什么以及如何解决?

0 个答案:

没有答案