gpt4 book ai didi

c# - 通过访问数据库来测试 Entity Framework 数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:48 25 4
gpt4 key购买 nike

我正在尝试通过实际访问数据库来测试我的真实数据。我实际上正在测试我的存储库类。这是我正在做的一个例子;

/// <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 和实际生产数据。

最佳答案

如果其他人(甚至您)进入您的数据库并创建一个新的批准国家或更改您的国家名称,您的测试将失败。你会想:“我的存储库有问题,为什么它没有按预期工作?”但是,是的,问题不在于存储库。

当我编写访问数据库的测试时,我喜欢创建数据库并在启动时加载默认值,然后销毁,然后就可以了。我不确定这是否是最佳选择,但效果很好。这种方法的问题是速度较慢,而且需要编写更多代码。

关于c# - 通过访问数据库来测试 Entity Framework 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7229498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com