【问题标题】:Error when trying to 'Seed' mock db into mock dbContext尝试将模拟数据库“播种”到模拟 dbContext 时出错
【发布时间】:2020-08-14 01:32:13
【问题描述】:

我一直在寻找堆栈溢出问题的解决方案,但我还没有找到能够修复我的错误的解决方案。我正在尝试为我开发的 API 编写单元测试。我为它创建了一个模拟数据库和模拟上下文,但是当我尝试“播种”我的模拟上下文时,我收到了这个错误。

The call is ambiguous between the following methods or properties: 'AppointmentAPI.UnitTests.DbContextExtensions.Seed(AppointmentAPI.Appt_Models.ApptSystemContext)' and 'AppointmentAPI.UnitTests.DbContextExtensions.Seed(AppointmentAPI.Appt_Models.ApptSystemContext)' [AppointmentAPI.UnitTests, AppointmentAPI.UnitTests]

不太确定是什么问题,因为前几天它工作正常,没有错误,然后当我今天开始处理它时,出现了错误。我对 C# 相当陌生,尤其是为 .net API 编写单元测试,因此非常感谢任何帮助。我将在下面发布我的两个文件。

DbContextExtensions.cs

namespace AppointmentAPI.UnitTests
{
    using System;
    using AppointmentAPI.Appt_Models;

    public static class DbContextExtensions
    {
        public static void Seed(this ApptSystemContext dbContext)
        {
            // add entities for dbContext instance
            dbContext.AppointmentSlots.Add(new AppointmentSlots
            {
                SlotId = 1,
                Date = Convert.ToDateTime("2020-03-31 00:00:00.000"),
                Time = TimeSpan.Parse("12:00:00.0000000"),
                ApptJson = "{'fname':'Billy','lname':'Joel','age':70,'caseWorker':'Donna', 'appStatus':'finished'}",
                Timestamp = Convert.ToDateTime("2020-02-24 12:00:00.000")
            });

            dbContext.AppointmentSlots.Add(new AppointmentSlots
            {
                SlotId = 6,
                Date = Convert.ToDateTime("2020-07-24 00:00:00.000"),
                Time = TimeSpan.Parse("10:00:00.0000000"),
                ApptJson = "{'fname':'Michael','lname':'Smith','age':52,'caseWorker':'Donna', 'appStatus':'finished'}",
                Timestamp = Convert.ToDateTime("2020-06-25 09:34:00.000")
            });

            dbContext.SaveChanges();
         }
      }
   }

DbContextMocker.cs

namespace AppointmentAPI.UnitTests
{
    using Microsoft.EntityFrameworkCore;
    using AppointmentAPI.Appt_Models;

    public static class DbContextMocker
    {
        public static ApptSystemContext GetApptSystemContext(string dbName)
        {
            // create option for DbContext instance
            var options = new DbContextOptionsBuilder<ApptSystemContext>()
                                        .UseInMemoryDatabase(databaseName: dbName)
                                        .Options;

            // create instance of DbContext
            var dbContext = new ApptSystemContext(options);

            // add entities in memory
            dbContext.Seed(); <-- error happens here

            return dbContext;
        }
    }
}

【问题讨论】:

    标签: c# asp.net visual-studio api xunit


    【解决方案1】:

    当使用相同数量但类型不同的两个或多个方法重载时,通常会引发此异常

    例如:

    static void Seed(this ApplicationDbContext dbContext, string word1, string[] array = null);
    static void Seed(this ApplicationDbContext dbContext, string word1, string word2 = null);
    
    dbContext.Seed("test word"); // will throw the exception or will show as an syntax error.
    
    

    对于上述两个文件,我看不到任何方法。干净和构建可能会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-27
      • 2021-02-11
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 2021-01-18
      相关资源
      最近更新 更多