【发布时间】: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