NUnit 测试失败,因为对象已在内存中
NUnit test fails because object is already in memory
我正在尝试 运行 几个单元测试(通过按 运行 All in Visual Studio)。当我 运行 all using Test>运行>All Tests 时,下面的测试失败了,但是当我 运行 单个测试(通过在测试资源管理器中右键单击测试本身)时,它成功了。我相信我的 Setup 和 TearDown 配置有误,但我不确定缺少什么。
错误:
Message: System.ArgumentException : An item with the same key has already been added. Key: System.Object[]
我的代码:
[TestFixture]
public class GCLandingUserModelTest
{
DbContextOptions<GreenCardContext> gcopt = new DbContextOptionsBuilder<GreenCardContext>()
.UseInMemoryDatabase(databaseName: "GreenCardSite").Options;
private GreenCardContext _mockGC;
[SetUp]
public void InitData()
{
_mockGC = new GreenCardContext(gcopt);
}
[TearDown]
public void ClearData()
{
_mockGC = null;
}
[Test]
public void TestAddObjMoq()
{
// Insert seed data into the database using one instance of the context
_mockGC.UserRolePrice.Add(new UserRolePrice { LGID = 1, UserRoleId = -1 });
_mockGC.SaveChanges();
Assert.AreEqual(1, _mockGC.UserRolePrice.ToList().Count, $"UserRolePrice not added to context");
//verify that obj created and changes were saved on the Mock Context
int objlgid = _mockGC.UserRolePrice.Where(x => x.UserRoleId == -1).Select(x => x.LGID).First();
Assert.AreEqual(1, objlgid,$"The returned lgid was: {objlgid}");
}
}
也许您需要清除内存中的 数据存储。
_mockGC.Database.EnsureDeleted();
DatabaseFacade.EnsureDeleted Method
Ensures that the database for the context does not exist. If it does
not exist, no action is taken. If it does exist then the database is
deleted.
Warning: The entire database is deleted, and no effort is made to
remove just the database objects that are used by the model for this
context.
我正在尝试 运行 几个单元测试(通过按 运行 All in Visual Studio)。当我 运行 all using Test>运行>All Tests 时,下面的测试失败了,但是当我 运行 单个测试(通过在测试资源管理器中右键单击测试本身)时,它成功了。我相信我的 Setup 和 TearDown 配置有误,但我不确定缺少什么。
错误:
Message: System.ArgumentException : An item with the same key has already been added. Key: System.Object[]
我的代码:
[TestFixture]
public class GCLandingUserModelTest
{
DbContextOptions<GreenCardContext> gcopt = new DbContextOptionsBuilder<GreenCardContext>()
.UseInMemoryDatabase(databaseName: "GreenCardSite").Options;
private GreenCardContext _mockGC;
[SetUp]
public void InitData()
{
_mockGC = new GreenCardContext(gcopt);
}
[TearDown]
public void ClearData()
{
_mockGC = null;
}
[Test]
public void TestAddObjMoq()
{
// Insert seed data into the database using one instance of the context
_mockGC.UserRolePrice.Add(new UserRolePrice { LGID = 1, UserRoleId = -1 });
_mockGC.SaveChanges();
Assert.AreEqual(1, _mockGC.UserRolePrice.ToList().Count, $"UserRolePrice not added to context");
//verify that obj created and changes were saved on the Mock Context
int objlgid = _mockGC.UserRolePrice.Where(x => x.UserRoleId == -1).Select(x => x.LGID).First();
Assert.AreEqual(1, objlgid,$"The returned lgid was: {objlgid}");
}
}
也许您需要清除内存中的 数据存储。
_mockGC.Database.EnsureDeleted();
DatabaseFacade.EnsureDeleted Method
Ensures that the database for the context does not exist. If it does not exist, no action is taken. If it does exist then the database is deleted.
Warning: The entire database is deleted, and no effort is made to remove just the database objects that are used by the model for this context.