C# 我的数据库集<;实体>;韩元';从单元测试(MSTest)代码尝试时,无法更新
我正在对我的MVC应用程序进行单元测试。测试之一是验证控制器是否正确返回所提供id的数据 为了对其进行单元测试,我想将[TestMethod]中的数据添加到Context.DBSet中,然后调用我的控制器方法并验证其输出 但是,我添加的记录没有反映在Context.DBSet对象中 下面是我目前拥有的代码C# 我的数据库集<;实体>;韩元';从单元测试(MSTest)代码尝试时,无法更新,c#,asp.net-mvc,entity-framework,mstest,dbcontext,C#,Asp.net Mvc,Entity Framework,Mstest,Dbcontext,我正在对我的MVC应用程序进行单元测试。测试之一是验证控制器是否正确返回所提供id的数据 为了对其进行单元测试,我想将[TestMethod]中的数据添加到Context.DBSet中,然后调用我的控制器方法并验证其输出 但是,我添加的记录没有反映在Context.DBSet对象中 下面是我目前拥有的代码 ///for purpose of unit testing, there is no underlying database public class Application
///for purpose of unit testing, there is no underlying database
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
{
Database.SetInitializer(new ApplicationDbContextInitializer());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Book> Books { get; set; }
public DbSet<Customer> Customers { get; set; }
}
最后我的MST测试
[TestMethod]
public void TestCustomerIDReplacedCorrectly()
{
var objCtrl = new CustomersController();
Customer objCust = new Customer()
{
Name = "Foo Bar",
Address = "Hello World",
};
Console.WriteLine(objCtrl.db.Customers.Count()) //returns 4 correctly
objCtrl.db.Customers.Add(objCust);
Console.WriteLine(objCtrl.db.Customers.Count()) //still returns 4!? it should return 5
///calls are given to controller method
///to fetch ViewResult and ViewBag
///to validate if it is processing the
///instance put inside the objCtrl.db.Customers
}
我看到添加的客户并没有在客户的DbContext对象中反映出来,因此,即使是我要验证的实际控制器方法也不能反映我想要的行为。如何确保将我的objCust
添加到objCtrl.db.Customers.Add
下面是为种子设定调用的InitializeCustomers()代码
private void InitializeCustomers(ApplicationDbContext context)
{
Customer cust1 = new Customer()
{
Name = "James Butt",
Address = "6649 N Blue Gum St",
Contact = "50-621 8927",
NationalID = "12312312312"
};
Customer cust2 = new Customer()
{
Name = "Josephine Darakjy",
Address = "Chanay, Jeffrey A Esq",
Contact = "81-292 9840",
NationalID = "123123124"
};
Customer cust3 = new Customer()
{
Name = "Art Venere",
Address = "Chemel, James L Cpa",
Contact = "85-636 8749",
NationalID = "123123456"
};
Customer cust4 = new Customer()
{
Name = "Lenna Paprocki",
Address = "Feltz Printing Service",
Contact = "90-385 4412",
NationalID = "32165498712"
};
context.Customers.Add(cust1);
context.Customers.Add(cust2);
context.Customers.Add(cust3);
context.Customers.Add(cust4);
}
您需要调用
objCtrl.db.SaveChanges()要添加到DbSet
的对象的code>。当前,您没有这样做,objCtrl.db.Customers.Count()
查询数据库中当前存储的对象的计数。由于您尚未调用SaveChanges
,因此您的对象尚未写入数据库,并且不会包含在.Count()
方法调用中
通常使用一个单独的db,它只用于测试,并添加了测试所需的数据(如果需要的话)。通常,您会看到每个测试都包装在一个事务中,这样您就可以执行所需的测试、验证数据,然后回滚所有操作,这样数据库就可以回到每个测试的开始状态您的InitializeBooks
和InitializeCustomers
方法是否按预期工作?如果是这样,请至少发布其中一个。是的,它们正确地为数据设定种子。因此,当我在我的[TestMethod()]
中执行Count()
时,它们反映了4正确地在末尾添加了代码。实际上,您并不是在对控制器进行单元测试。您正在测试数据库。在测试中,您没有在控制器上调用任何方法,只是访问面向公众的db
属性并直接使用数据库。如果这是你真正想要做的,那很好,但在那一点上,你正在测试EF,这并没有任何真正的目的。要将此作为控制器的单元测试,您需要实际测试控制器方法,此时您的数据库应该完全模拟出来,并且您只测试应该发生的行为是否发生。callSaveChanges()
在添加
函数之后,我故意缩短了MSTest,因为问题越来越长了。该测试实际上是命中控制器方法,以验证我在DB对象中添加的对象是否根据BusinessRules进行处理。我检查这个方法返回的ViewResult和ViewBag对象没有DB的情况下,值是如何种子化的呢?然后,我现在关注的是基础上的rite.seed(),如ApplicationDBContactExiteInitializer
中所示,不知怎的,如果我能得到它,我会尝试调用种子(dbObject)
@AvdhutVaidya这似乎是一种非常迂回的解决问题的方法。我猜(因为它没有发布)您的createDatabaseIfnoteExists
类调用中的Seed
方法。SaveChanges
在它自身的某个地方。。。要纠正问题,您只需在测试方法中调用。SaveChanges
,以解决问题I与此想法略有不同;这是因为当前调用了Seed()
,并且没有数据库连接到我的应用程序.SaveChanges()
会使程序崩溃。我无权访问种子()
,它只是继承的。此外,我必须使用CreateDatabaseIfNotExists
或DropCreateAlways
或DropCreateIfModelChanges
,因为EF需要我来编写它。GregH,我的问题可能已经解决了,我尝试了.SaveChanges()
,但它没有给我错误。这是令人惊讶的。从昨天开始,我一直在.SaveChanges()
上收到异常,但我没有注意到什么异常。为了在这里说明这个异常,我使用.SaveChanges()
重新运行了测试,它返回了额外的记录!!让我再检查一遍。
[TestMethod]
public void TestCustomerIDReplacedCorrectly()
{
var objCtrl = new CustomersController();
Customer objCust = new Customer()
{
Name = "Foo Bar",
Address = "Hello World",
};
Console.WriteLine(objCtrl.db.Customers.Count()) //returns 4 correctly
objCtrl.db.Customers.Add(objCust);
Console.WriteLine(objCtrl.db.Customers.Count()) //still returns 4!? it should return 5
///calls are given to controller method
///to fetch ViewResult and ViewBag
///to validate if it is processing the
///instance put inside the objCtrl.db.Customers
}
private void InitializeCustomers(ApplicationDbContext context)
{
Customer cust1 = new Customer()
{
Name = "James Butt",
Address = "6649 N Blue Gum St",
Contact = "50-621 8927",
NationalID = "12312312312"
};
Customer cust2 = new Customer()
{
Name = "Josephine Darakjy",
Address = "Chanay, Jeffrey A Esq",
Contact = "81-292 9840",
NationalID = "123123124"
};
Customer cust3 = new Customer()
{
Name = "Art Venere",
Address = "Chemel, James L Cpa",
Contact = "85-636 8749",
NationalID = "123123456"
};
Customer cust4 = new Customer()
{
Name = "Lenna Paprocki",
Address = "Feltz Printing Service",
Contact = "90-385 4412",
NationalID = "32165498712"
};
context.Customers.Add(cust1);
context.Customers.Add(cust2);
context.Customers.Add(cust3);
context.Customers.Add(cust4);
}