如何防止在单元测试中自动包含导航属性

我目前正在开发一个应用程序商店风格的 API,它具有以下实体(以及许多其他实体,但与问题无关):

  • App(与 AppRevision 的一对多关系 - 包含 IEnumerable 属性)

  • 应用程序修订版

  • 安装

我遇到了一个奇怪的问题,其中 EF 的行为在单元测试中与实际运行 API 时不同,因为在单元测试时会自动包含导航属性。

从我的命令处理程序中获取以下代码片段:

// MyComp


<div id=“@Id” @attributes=“InputAttributes”></div>


@code {

    [Parameter] 

    public string Id { get; set; } 


    [Parameter(CaptureUnmatchedValues = true)]

    public Dictionary<string, object> InputAttributes { get; set; }

}

根据上面的示例定义参数将使其收集组件上定义的与任何现有声明的参数不匹配的任何属性。


用法:


<MyComp Id=“foo” class=“myclass” />

将呈现:


<div id=“foo” class=“myclass”></div>


慕盖茨4494581
浏览 114回答 1
1回答

慕容3067478

对于将来遇到这篇文章的任何人,解决方案是根据原始问题的评论,使用单独的上下文来播种测试数据并稍后在测试中获取数据:[Fact]public async void Handle_ShouldAddInstallationRecord_WhenDataIsValid(){&nbsp; &nbsp; Guid testGuid = Guid.NewGuid();&nbsp; &nbsp; CreateInstallationCommand command = new CreateInstallationCommand(testGuid, "ABC", "abc@abc.com", null);&nbsp; &nbsp; using (TestContextFactory contextFactory = new TestContextFactory())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (TestContext seedContext = contextFactory.CreateTestContext())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seedContext.Apps.Add(new App() { Id = testGuid });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seedContext.AppRevisions.Add(new AppRevision() { Id = Guid.NewGuid(), AppId = testGuid, Status = AppRevisionStatus.Approved, IsListed = true });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await seedContext.SaveChangesAsync();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; using (TestContext getContext = contextFactory.CreateTestContext())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreateInstallationCommandHandler handler = new CreateInstallationCommandHandler(getContext);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CommandResult result = await handler.Handle(command, new CancellationToken());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.True(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.Single(getContext.Installations);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP