我想通过实际点击数据库来测试我的真实数据。我实际上正在测试我的Repository类。这是我正在做的一个例子;通过点击数据库测试实体框架数据
/// <summary>
/// Summary description for Country
/// </summary>
[TestClass]
public class Country {
public Country() {
_countryRepo = new CountryRepository();
}
private ICountryRepository _countryRepo;
[TestMethod]
public void db_should_return_at_least_one_country_as_approved_all() {
//Act
var model = _countryRepo.GetAll();
//Assert
Assert.IsTrue(model.Count() >= 1);
}
[TestMethod]
public void db_should_return_at_least_one_country_as_approved_true() {
//Act
var model = _countryRepo.GetAll(ApprovalStatus.Approved);
//Assert
Assert.IsTrue(model.Count() >= 1);
}
[TestMethod]
public void db_should_return_Turkey_as_country_name_by_id() {
//Act
var model = _countryRepo.GetSingle(1000);
//Assert
Assert.AreEqual<string>("Turkey", model.CountryName);
}
[TestMethod]
public void db_should_return_Turkey_as_country_name_by_countryISO3166Code() {
//Act
var model = _countryRepo.GetSingle("TR");
//Assert
Assert.AreEqual<string>("Turkey", model.CountryName);
}
[TestMethod]
public void db_should_return_Turkey_as_country_name_by_GUID() {
//Act
var model = _countryRepo.GetSingle(Guid.Parse("9AF174A6-D0F7-4393-AAAD-B168BADEDB30"));
//Assert
Assert.AreEqual<string>("Turkey", model.CountryName);
}
}
这对我的需求很好,但是想知道我是否正在按照本书进行操作。还有其他的模式,我真的应该在这里。我不想伪造我的数据,这里真正激烈的是测试我的DAL和真实的生产数据。
是的,你是对的,如果别人会去餐桌并将土耳其国家名称更改为别的东西,它会中断。但我这里激烈的是测试我的EF代码是否正常工作。所以,你建议将所有的值加载到变量中,然后再使用它们?它会带来什么样的差异? – tugberk
不是变量。我在启动时运行SQL来创建数据库和数据。之后,我的测试运行,它会检查知识库是否返回预期值。我确信数据将在那里,并且我知道该如何预期,因为我已经写了SQL(即在测试代码中)。不同之处在于你不太依赖数据库的当前状态。如果某些测试更改了某一行的状态,则需要将其回滚,因为下次运行它时,测试可能会失败。 – goenning
好吧。所以制作数据库的副本并在本地运行它也可以使用假数据,对吧? – tugberk