Poco 的 FakeItEasy 假实例 class

FakeItEasy fake instansiation of a Poco class

我有一个 class:

public class WeekCompareModel 
{
   public WeekData Week1 {get; set;}
   public WeekData Week1 {get; set;}  
}

这个对象是这样创建的:

var model = new WeekCompareModel
{
   Week1 = Repo.FirstOrDefault(x => x.Typ == (int)type && x.Week == week1),
   Week2 = Repo.FirstOrDefault(x => x.Typ == (int)type && x.Week == week2),
};

在我的代码中,这是在实现基础 class:

的管理器 class 中实例化的
public class SystemStatisticsManager : CrudManagerBase<Systemstatistik>, ISystemStatisticsManager
{
    public SystemStatisticsManager()
    {
        Repo = CoreRepository.Systemstatistiks;
    }
...
}

我的 crudmanagerbase class:

public class CrudManagerBase<T> : ICrudManagerBase<T>
    where T : class
{
    protected readonly ICachedCoreRepository CoreRepository;
    protected IQueryable<T> Repo;
 ...
 }

我想在我的单元测试中做的是覆盖对

的调用
 new WeekCompareModel { ... }

并将其替换为该模型的测试实例。

知道 FakeItEasy 或任何其他 Mock 库是否可行吗?

如@Nikosi 的评论所述,如果不将创建委托给另一种方法,FakeItEasy 将无法做到这一点。

但您也询问了其他图书馆。我以前用过 Typemock Isolator,它有这个功能。该功能称为 Faking Future Instances,可以像

一样使用
var handle = Isolate.Fake.NextInstance<Dependency>();

// Do something with the system under test

缺点是 Isolator 是商业产品,正如我输入的那样,costs about 9 US/year

哦。我刚刚注意到 JustMock 也有 Future Mocking as well. I can't tell from the documentation but assume it also only provides the feature in its paid version.