Tôi đang viết một tập hợp các bài kiểm tra tích hợp (Bài kiểm tra đơn vị với MS Test để kiểm tra Entity Framework 4.2 là duy trì tất cả các lớp một cách chính xác cho cơ sở dữ liệu).Kiểm tra tích hợp khung thực thể DropCreateDatabaseAlways không xóa cơ sở dữ liệu giữa thử nghiệm
Khi tôi chạy tất cả các bài kiểm tra từng người một, tất cả đều hoạt động tốt. Khi tôi chạy chúng trong một nhóm - một số trong số chúng thất bại vì số lượng các đối tượng sai được trả về - có vẻ như db đang được làm sạch một lần khi bắt đầu các phép thử và không ở giữa mỗi phép thử - mặc dù tôi có thể thấy bối cảnh mới được tạo và sau đó được xử lý cho từng thử nghiệm
Bất kỳ ý tưởng nào?
public class EmptyDataInitializer : DropCreateDatabaseAlways<myContext>
{
protected override void Seed(myContext db)
{
//Do Nothing Create Empty Database
db.SaveChanges();
base.Seed(db);
}
}
Một phiên bản Cắt giảm của đơn vị/hội nhập Tests
[TestClass]
public class PersistanceTests
{
//Creating two instances of our Repository so that we can make sure that we are reading from our database rather than in-memory
private myContext _db;
private myContext _dbResults;
private readonly ISettings _configSettings;
public PersistanceTests()
{
_configSettings = MockRepository.GenerateStub<ISettings>();
_configSettings.ConnectionString = "data source=.;initial catalog=myContext_Test; Integrated Security=SSPI; Pooling=false";
Database.SetInitializer(new EmptyDataInitializer());
}
//This is called a single time after the last test has finished executing
[TestCleanup]
public void TearDownTest()
{
_db.Dispose();
_db = null;
_dbResults.Dispose();
_dbResults = null;
}
//This is called each time prior to a test being run
[TestInitialize]
public void SetupTest()
{
_db = new myContext(_configSettings);
_dbResults = new myContext(_configSettings);
// This forces the database to initialise at this point with the initialization data/Empty DB
var count = _db.Accounts.Count();
var resultCount = _dbResults.Accounts.Count();
if (count != resultCount) throw new InvalidOperationException("We do not have a consistant DB experiance.");
}
[TestMethod]
public void OrganisationPersistanceTest()
{
// Arrange
var apple = new Organisation { Name = "Apple" };
_db.Organisations.Add(apple);
// Act
_db.SaveChanges();
var organisationsCount = _dbResults.Organisations.Count();
var organisationsAppleCount = _dbResults.Organisations.Where(a => a.Id == apple.Id).Count();
var result = _dbResults.Organisations.FirstOrDefault(a => a.Id == apple.Id);
// Assert
Assert.IsTrue(organisationsCount == 1, string.Format("Organisations Count Mismatch - Actual={0}, Expected={1}", organisationsCount, 1));
Assert.IsTrue(organisationsAppleCount == 1, string.Format("Apple Organisations Count Mismatch - Actual={0}, Expected={1}", organisationsAppleCount, 1));
Assert.IsNotNull(result, "Organisations Result should not be null");
Assert.AreEqual(result.Name, apple.Name, "Name Mismatch");
}
//A Unit test
[TestMethod]
public void OrganisationWithNumberOfPeople_PersistanceTest()
{
// Arrange
var person = new Person { Firstname = "Bea" };
var anotherPerson = new Person { Firstname = "Tapiwa" };
var apple = new Organisation { Name = "Apple" };
apple.AddPerson(person);
apple.AddPerson(anotherPerson);
_db.Organisations.Add(apple);
// Act
_db.SaveChanges();
var organisationsCount = _dbResults.Organisations.Count();
var organisationsAppleCount = _dbResults.Organisations.Where(a => a.Id == apple.Id).Count();
var result = _dbResults.Organisations.FirstOrDefault(a => a.Id == apple.Id);
var peopleCountInOrganisation = result.People.Count();
// Assert
Assert.IsTrue(organisationsCount == 1, string.Format("Organisations Count Mismatch - Actual={0}, Expected={1}", organisationsCount, 1));
Assert.IsTrue(organisationsAppleCount == 1, string.Format("Apple Organisations Count Mismatch - Actual={0}, Expected={1}", organisationsAppleCount, 1));
Assert.IsNotNull(result, "Organisations Result should not be null");
Assert.AreEqual(result.People.Count, peopleCountInOrganisation, "People count mismatch in organisation Apple - Actual={0}, Expected={1}", peopleCountInOrganisation, 2);
Assert.AreEqual(result.Name, apple.Name, "Name Mismatch");
}
}
Đẩy mạnh thông qua các bài kiểm tra tôi có thể thấy SetupTest và phương pháp TearDownTest được gọi nhưng tôi nó dường như không làm sạch cơ sở dữ liệu giữa các bài kiểm tra.
Okay Dokey nó sẽ có vẻ một câu trả lời (mặc dù có vẻ hơi khó hiểu với tôi) là sửa đổi phương pháp TestCleanup để nó giảm xuống và tái tạo cơ sở dữ liệu sau mỗi lần kiểm tra [TestCleanup] công khai v oid TearDownTest() { nếu (_db.Database.Exists()) { _db.Database.Delete(); _db.Database.CreateIfNotExists(); } _db.Dispose(); _db = null; _dbResults.Dispose(); _dbResults = null; } –
Được rồi, ngay cả Câu trả lời hay hơn - thêm cơ sở dữ liệu.Initialize (force: true); vào phương pháp TestInitialize. [TestInitialize] void công khai SetupTest() { _db = new myContext (_configSettings); _db.Database.Initialize (force: true); –