亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何防止在單元測試中自動包含導航屬性

如何防止在單元測試中自動包含導航屬性

C#
慕蓋茨4494581 2023-08-20 10:21:40
我目前正在開發一個應用程序商店風格的 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>
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

對于將來遇到這篇文章的任何人,解決方案是根據原始問題的評論,使用單獨的上下文來播種測試數據并稍后在測試中獲取數據:


[Fact]

public async void Handle_ShouldAddInstallationRecord_WhenDataIsValid()

{

    Guid testGuid = Guid.NewGuid();


    CreateInstallationCommand command = new CreateInstallationCommand(testGuid, "ABC", "[email protected]", null);


    using (TestContextFactory contextFactory = new TestContextFactory())

    {

        using (TestContext seedContext = contextFactory.CreateTestContext())

        {

            seedContext.Apps.Add(new App() { Id = testGuid });

            seedContext.AppRevisions.Add(new AppRevision() { Id = Guid.NewGuid(), AppId = testGuid, Status = AppRevisionStatus.Approved, IsListed = true });

            await seedContext.SaveChangesAsync();

        }


        using (TestContext getContext = contextFactory.CreateTestContext())

        {

            CreateInstallationCommandHandler handler = new CreateInstallationCommandHandler(getContext);


            CommandResult result = await handler.Handle(command, new CancellationToken());


            Assert.True(result);

            Assert.Single(getContext.Installations);

        }

    }

}


查看完整回答
反對 回復 2023-08-20
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號