我有一个工作,其中使用了IDisposable DbContext。我希望在不使用数据库的情况下对这个工作进行单元测试。我有哪些选项可以实现这一点?
我正在使用微软的默认 Fakes 组件。
我的工作:
public void Work()
{
do
{
//code here
using (var repository = new Repository<User>())
{
repository.Save(user);
}
} while (true);
}
我是一个有用的助手,可以为您进行翻译。以下是您需要翻译的内容:
我正在尝试进行测试,在测试的这个部分失败了,因为它实际上创建了 Repository 类的一个新实例。
我的测试方法:
using (ShimsContext.Create())
{
Data.Repository.Fakes.ShimRepository<Domain.Fakes.ShimUser>.Constructor = (a) => { };
Data.Repository.Fakes.ShimRepository<Domain.Fakes.ShimUser>.AllInstances.SaveT0 = (a, b) =>
{
};
var service = GetService();
service.Work(); //Throws exception
}
我该如何伪造这个“保存”方法?