Oop 如何编写紧密耦合的工厂设计模式代码块的Xunit测试用例?
我想编写以下方法的xunit测试用例。您能建议替代设计吗?这样我就可以在当前项目中以最小的更改编写xunit测试用例Oop 如何编写紧密耦合的工厂设计模式代码块的Xunit测试用例?,oop,design-patterns,c#-4.0,solid-principles,xunit.net,Oop,Design Patterns,C# 4.0,Solid Principles,Xunit.net,我想编写以下方法的xunit测试用例。您能建议替代设计吗?这样我就可以在当前项目中以最小的更改编写xunit测试用例 public ActionResult Index(int id = 0, AssetFilterType filter = AssetFilterType.All) { using (var tracer = new Tracer("AssetController", "Index"))
public ActionResult Index(int id = 0, AssetFilterType filter = AssetFilterType.All)
{
using (var tracer = new Tracer("AssetController", "Index"))
{
RemoveReturnUrl();
ViewBag.JobId = id;
var response = ContextFactory.Current.GetDomain<EmployeeDomain>().GetEmployeeFilterAsync(id,
CurrentUser.CompanyId, filter); // Not able write unit test case , please suggest alternate design.
return View("View", response);
}
}
公共操作结果索引(int id=0,AssetFilterType筛选器=AssetFilterType.All)
{
使用(var跟踪程序=新跟踪程序(“资产控制器”、“索引”))
{
RemoveTurnUrl();
ViewBag.JobId=id;
var response=ContextFactory.Current.GetDomain().GetEmployeeFilterAsync(id,
CurrentUser.CompanyId,filter);//无法编写单元测试用例,请建议替代设计。
返回视图(“视图”,响应);
}
}
当前设计如下所示
public interface IDomain
{
}
public interface IContext
{
D GetDomain<D>() where D : IDomain;
string ConnectionString { get; }
}
public class ApplicationContext : IContext
{
public D GetDomain<D>() where D : IDomain
{
return (D)Activator.CreateInstance(typeof(D));
}
public string ConnectionString
{
get
{
return "DatabaseConnection";
}
}
}
public class ContextFactory
{
private static IContext _context;
public static IContext Current
{
get
{
return _context;
}
}
public static void Register(IContext context)
{
_context = context;
}
}
公共接口IDomain
{
}
公共接口IContext
{
D GetDomain(),其中D:IDomain;
字符串连接字符串{get;}
}
公共类应用程序上下文:IContext
{
public D GetDomain(),其中D:IDomain
{
返回(D)Activator.CreateInstance(typeof(D));
}
公共字符串连接字符串
{
得到
{
返回“数据库连接”;
}
}
}
公共类上下文工厂
{
私有静态IContext\u上下文;
公共静态IContext当前
{
得到
{
返回上下文;
}
}
公共静态无效寄存器(IContext上下文)
{
_上下文=上下文;
}
}
//var response=ContextFactory.Current.GetDomain**此行用于从EmployeeDomain调用特定类方法,即GetEmployeeFilterAsync。虽然它是非常方便和广泛应用于我们的应用程序,但由于设计问题,我不能写的单位 测试用例 请您建议设计,这样我们就可以用最小的更改编写单元测试用例。不要使用,而是使用构造函数注入。我无法从OP中分辨出什么是
AssetDomain
,但似乎重要的是依赖性。将其注入到类中:
public class ProbablySomeController
{
public ProbablySomeController(AssetDomain assetDomain)
{
AssetDomain = assetDomain;
}
public AssetDomain AssetDomain { get; }
public ActionResult Index(int id = 0, AssetFilterType filter = AssetFilterType.All)
{
using (var tracer = new Tracer("AssetController", "Index"))
{
RemoveReturnUrl();
ViewBag.JobId = id;
var response = AssetDomain.GetAssetFilterAsync(id, CurrentUser.CompanyId, filter);
return View("View", response);
}
}
}
假设AssetDomain
是多态类型,现在可以编写测试并注入:
非常感谢Mark的快速响应,EmployeeDomain就是这样一个类,它有许多方法,并通过IDomain接口继承。我们有多种类型的xxxDomain类文件
[Fact]
public void MyTest()
{
var testDouble = new AssetDomainTestDouble();
var sut = new ProbablySomeController(testDouble);
var actual = sut.Index(42, AssetFilterType.All);
// Put assertions here
}