我正在尝试为我的存储库实施集成测试,但在每次集成测试之前,我都选择了正确的策略来插入测试数据。
这就是我的存储库测试类的样子;
[TestFixture]
public class RealtyTest : ITest<IRealtyRepository>
{
public IUnitOfWork Uow { get; set; }
public IRealtyRepository Repository { get; set; }
public ITestEnvironment TestEnvironment { get; set; }
[OneTimeSetUp]
public void OneTimeSetup()
{
TestEnvironment = new RealtyTestEnvironment();
TestEnvironment.Prepare();
}
[SetUp]
public void Setup()
{
Uow = AppCore.Instance.RealtyUow;
Uow.BeginTransaction();
Repository = ((IRealtyUow)Uow).RealtyRepository;
}
[Test]
public void should_get_realty_detail_by_id()
{
Realty realty = Repository.GetDetail(1);
Assert.IsNotNull(realty.Firm);
Assert.IsNotNull(realty.FirmUser);
Assert.IsNotNull(realty.Category);
Assert.IsNotNull(realty.SubCategory);
Assert.IsNotNull(realty.Publish);
Assert.IsNotNull(realty.ResIdence);
Assert.IsNotNull(realty.Star);
Assert.IsNotNull(realty.Floor);
Assert.IsNotNull(realty.Heating);
Assert.IsNotNull(realty.Fuel);
Assert.IsNotNull(realty.BuildState);
Assert.IsNotNull(realty.Usage);
Assert.IsNotNull(realty.Credit);
Assert.IsNotNull(realty.Register);
Assert.IsNotNull(realty.Activate);
Assert.IsNotNull(realty.District);
Assert.IsNotNull(realty.District.County);
Assert.IsNotNull(realty.District.County.City);
Assert.IsNotNull(realty.District.County.City.Country);
}
[Test]
public void should_get_realty_detail_all_by_id()
{
Realty realty = Repository.GetWithChilds(1);
Assert.IsNotNull(realty.Firm);
Assert.IsNotNull(realty.FirmUser);
Assert.IsNotNull(realty.Category);
Assert.IsNotNull(realty.SubCategory);
Assert.IsNotNull(realty.Publish);
Assert.IsNotNull(realty.ResIdence);
Assert.IsNotNull(realty.Star);
Assert.IsNotNull(realty.Floor);
Assert.IsNotNull(realty.Heating);
Assert.IsNotNull(realty.Fuel);
Assert.IsNotNull(realty.BuildState);
Assert.IsNotNull(realty.Usage);
Assert.IsNotNull(realty.Credit);
Assert.IsNotNull(realty.Register);
Assert.IsNotNull(realty.Activate);
Assert.IsNotNull(realty.District);
Assert.IsNotNull(realty.District.County);
Assert.IsNotNull(realty.District.County.City);
Assert.IsNotNull(realty.District.County.City.Country);
Assert.Greater(realty.Files.Count, 0);
Assert.Greater(realty.Attributes.Count, 0);
}
[TearDown]
public void TearDown()
{
if (Uow != null)
{
Uow.Rollback();
Uow.Dispose();
}
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
TestEnvironment.Rollback();
}
}
正如您在OneTimeSetup()方法上看到的那样,我在测试类中完成每个集成测试方法之后插入测试数据并删除OneTimeTearDown()方法。 对于测试数据创建,我使用了NDbUnit库。
这是我的RealtyTestEnvironment类实现;
public class RealtyTestEnvironment : ITestEnvironment
{
private NDbUnitTest _database = null;
public int TestRowCount { get; set; }
public RealtyTestEnvironment()
{
TestRowCount = 1;
}
public void Prepare()
{
_database = new SqlDbUnitTest(TestSettings.HemlakTestDbConnection);
_database.ReadXmlSchema(string.Format(@"{0}\HemlakDb\Hemlak.xsd", TestSettings.AppRootPath));
_database.PerformDbOperation(DbOperationFlag.DeleteAll);
GeneralTestDataBuilder generalDataBuilder = new GeneralTestDataBuilder();
_database.ReadXml(generalDataBuilder.GetTestTypeStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
LocationTestDataBuilder locationDataBuilder = new LocationTestDataBuilder();
_database.ReadXml(locationDataBuilder.GetTestCountryStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
_database.ReadXml(locationDataBuilder.GetTestCityStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
_database.ReadXml(locationDataBuilder.GetTestCountyStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
_database.ReadXml(locationDataBuilder.GetTestDistrictStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
FirmTestDataBuilder firmDataBuilder = new FirmTestDataBuilder();
_database.ReadXml(firmDataBuilder.GetTestFirmStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
_database.ReadXml(firmDataBuilder.GetTestFirmUserStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
RealtyTestDataBuilder realtyDataBuilder = new RealtyTestDataBuilder();
_database.ReadXml(realtyDataBuilder.GetTestRealtyStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
_database.ReadXml(realtyDataBuilder.GetTestRealtyFileStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
_database.ReadXml(realtyDataBuilder.GetTestRealtyAttributeStream(TestRowCount));
_database.PerformDbOperation(DbOperationFlag.InsertIdentity);
}
public void Rollback()
{
if (_database != null)
{
_database.PerformDbOperation(DbOperationFlag.DeleteAll);
}
}
}
所以我选择了NDbUnit,但我觉得不舒服,因为项目的最后提交日期是3年前,需要花费太多精力来准备测试环境。
有些人使用Repository类本身来插入测试数据,所以现在我们使用Repository类来插入测试数据来测试那个Repository类?这对我没有意义。
- 您的方法是什么,如何插入测试数据以测试您的存储库?
- 我的实施怎么样,我应该继续使用Ndbunit吗?