【发布时间】:2015-10-21 15:00:31
【问题描述】:
我们正在尝试将 Nunit 测试集成到我们的 Web 应用程序中。这里我们使用 Nsubstitute 作为模拟框架。 项目架构如下:
Public class BaseService : Glass.Mapper.Sc.SitecoreContext
{
public BaseService(){}
}
Public class DerivedService : BaseService
{
IGenericRepository<Item> _genericRepository;
public DerivedService ( IGenericRepository<Item> _repository)
{
_genericRepository= _repository;
}
public string DoSomethig(){}
}
现在为了测试我的 DerivedService 类的 DoSomething() 方法,我正在创建我的存储库的替代品并伪造它的响应。这应该让我测试我的服务代码。
[Test]
public void TestDoSomethigMethod()
{
var repository = Substitute.For<IGenericRepository<Item>>();
DerivedService tempService = new DerivedService(repository);
// Throws an exception of type System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary. at base service constructor.
var response = tempService.DoSomething();
}
当我尝试调用派生服务的实例时,它会在 baseService 构造函数中抛出异常(字典中不存在给定的键) 我们使用温莎城堡进行依赖注入,基类继承自 Glass Mapper 站点核心上下文类。 如果有人遇到任何此类问题或对此有解决方案,请告诉我。
编辑:按照 Pavel & Marcio 的建议更新了测试用例的代码。
【问题讨论】:
标签: c# .net nsubstitute glass-mapper