如何使用硬编码参数对方法进行单元测试?
How to unit test a method with hardcoded parameters?
我有一个class方法。此方法检查 assembly/namespace 并读取所有 class 中名称为 ProductModel 的所有 space:
public static IEnumerable<ProductModel?> GetProductModels()
{
var typesList = Assembly.GetExecutingAssembly()
.GetTypes().Where(x =>
x.Name.Contains("ProductModel") && // This is the name of the classes
x.FullName!.Contains("MyProject.Domain.ProductTypes")) // This is the namespace of the project
.ToList();
var ProductModelLists = typesList.Select(x =>
{
return GetProductModelMethod(x);
});
if (ProductModelLists == null)
{
throw new ArgumentException("No ProductModel Found");
}
else{
return ProductModelLists;
}
}
此 class 和方法与 ProductTypes/ProductModels - MyProject.Domain.ProductTypes.
在同一个项目和名称 space 中
我想使用 XUnit 测试 2 个场景 - 一个有 ProductModels,另一个有 none 并且抛出 Argument 异常。
由于这些值是硬编码在方法内部的,所以对我来说这几乎是不可能的。有没有人知道如何做类似的事情?
与实现细节的紧密耦合使得隔离该代码变得困难。考虑重构以使用抽象和显式依赖原则来使代码更可靠。
鉴于被测试的主题成员是一个 static
函数,我假设这是在某个静态实用程序中 class。核心功能仍然可以重构为它自己的功能,允许可以操作的输入。
public static IEnumerable<ProductModel?> GetProductModelsCore(IEnumerable<Type> types) {
var typesList = types.Where(x =>
x.Name.Contains("ProductModel") && // This is the name of the classes
x.FullName!.Contains("MyProject.Domain.ProductTypes")) // This is the namespace of the project
.ToList();
var ProductModelLists = typesList.Select(x => {
return GetProductModelMethod(x);
});
if (ProductModelLists == null) {
throw new ArgumentException("No ProductModel Found");
} else {
return ProductModelLists;
}
}
并从主函数调用
public static IEnumerable<ProductModel?> GetProductModels() =>
GetProductModelsCore(Assembly.GetExecutingAssembly().GetTypes());
可以使用满足所需场景的假类型单独测试核心功能。
同样,理想情况下,这应该转移到具有抽象的服务,这些服务允许更可靠的代码库,有助于更好地维护,但基于提供的原始代码,目前不在这个答案的范围内。
我有一个class方法。此方法检查 assembly/namespace 并读取所有 class 中名称为 ProductModel 的所有 space:
public static IEnumerable<ProductModel?> GetProductModels()
{
var typesList = Assembly.GetExecutingAssembly()
.GetTypes().Where(x =>
x.Name.Contains("ProductModel") && // This is the name of the classes
x.FullName!.Contains("MyProject.Domain.ProductTypes")) // This is the namespace of the project
.ToList();
var ProductModelLists = typesList.Select(x =>
{
return GetProductModelMethod(x);
});
if (ProductModelLists == null)
{
throw new ArgumentException("No ProductModel Found");
}
else{
return ProductModelLists;
}
}
此 class 和方法与 ProductTypes/ProductModels - MyProject.Domain.ProductTypes.
在同一个项目和名称 space 中我想使用 XUnit 测试 2 个场景 - 一个有 ProductModels,另一个有 none 并且抛出 Argument 异常。
由于这些值是硬编码在方法内部的,所以对我来说这几乎是不可能的。有没有人知道如何做类似的事情?
与实现细节的紧密耦合使得隔离该代码变得困难。考虑重构以使用抽象和显式依赖原则来使代码更可靠。
鉴于被测试的主题成员是一个 static
函数,我假设这是在某个静态实用程序中 class。核心功能仍然可以重构为它自己的功能,允许可以操作的输入。
public static IEnumerable<ProductModel?> GetProductModelsCore(IEnumerable<Type> types) {
var typesList = types.Where(x =>
x.Name.Contains("ProductModel") && // This is the name of the classes
x.FullName!.Contains("MyProject.Domain.ProductTypes")) // This is the namespace of the project
.ToList();
var ProductModelLists = typesList.Select(x => {
return GetProductModelMethod(x);
});
if (ProductModelLists == null) {
throw new ArgumentException("No ProductModel Found");
} else {
return ProductModelLists;
}
}
并从主函数调用
public static IEnumerable<ProductModel?> GetProductModels() =>
GetProductModelsCore(Assembly.GetExecutingAssembly().GetTypes());
可以使用满足所需场景的假类型单独测试核心功能。
同样,理想情况下,这应该转移到具有抽象的服务,这些服务允许更可靠的代码库,有助于更好地维护,但基于提供的原始代码,目前不在这个答案的范围内。