Asp.net web api 使用NInject向WebApi控制器提供Rhino模拟对象-存根始终返回null
我试图使用从[ClassInitialize](MS test)方法调用的Owin内存服务器对我的WebApi控制器进行单元测试。我需要通过DI容器将我的存储库对象IFourSquareRepository的模拟实例注入到我的控制器中。当测试类[ClassInitialize]方法执行时,Owin服务器设置、静态Ninject IKernel实例及其绑定在WebApi项目的Owin配置类中处理:Asp.net web api 使用NInject向WebApi控制器提供Rhino模拟对象-存根始终返回null,asp.net-web-api,ninject,owin,rhino-mocks,Asp.net Web Api,Ninject,Owin,Rhino Mocks,我试图使用从[ClassInitialize](MS test)方法调用的Owin内存服务器对我的WebApi控制器进行单元测试。我需要通过DI容器将我的存储库对象IFourSquareRepository的模拟实例注入到我的控制器中。当测试类[ClassInitialize]方法执行时,Owin服务器设置、静态Ninject IKernel实例及其绑定在WebApi项目的Owin配置类中处理: kernel.Bind<IFourSquareRepository&g
kernel.Bind<IFourSquareRepository>().ToMethod(
context =>
{
return MockRepository.GenerateMock<IFourSquareRepository>();
// This block runs only once ...
// But stubs from the test method return null when the test call
// fires up the controller ...
}
).InSingletonScope();
我的问题是,无论我做什么,当控制器(从上面的HttpClient请求调用)调用stubbed方法时,它总是返回NULL
public IEnumerable<BookmarkedPlace> Get(string userName, int page = 0, int pageSize = 10)
{
IQueryable<BookmarkedPlace> query;
query = this.Repository.GetFirstBookmarkedPlace();
// Mock Repo call returning null !
// Other stuff goes here ...
return results;
}
public IEnumerable Get(字符串用户名,int-page=0,int-pageSize=10)
{
可查询查询;
query=this.Repository.GetFirstBookmarkedPlace();
//模拟回购调用返回空!
//其他东西在这里。。。
返回结果;
}
我已经在这个问题上绞尽脑汁好几天了-有什么想法吗?我想原因是,当您创建模拟存储库时,后端必须使用某种上下文作为依赖项进行调用,这也会被打断,因此不会映射到实际的数据库集 在过去,我使用了下面的方法来模拟我的存储库,并在我的基本测试类中获得DbSet的一个实例(因为这在生成中是常见的)
公共静态IDbSet生成集(IList数据),其中T:class
{
IQueryable queryable=data.AsQueryable();
IDbSet dbSet=MockRepository.GenerateMock();
Stub(x=>x.Provider).Return(queryable.Provider);
Stub(x=>x.Expression).Return(queryable.Expression);
Stub(x=>x.ElementType).Return(queryable.ElementType);
Stub(x=>x.GetEnumerator()).Return(null).WhenCalled(x=>queryable.GetEnumerator());
返回dbSet;
}
然后在初始化的测试类中,我做:
[TestInitialize]
public new void Initialize()
{
base.Initialize();
_context = MockRepository.GenerateMock<YourContextInterface>();
_context.Stub(x => x.YourDbSet).PropertyBehavior();
_context.YourDbSet= GenerateSet(YourDbSet);
}
[测试初始化]
public new void Initialize()
{
base.Initialize();
_context=MockRepository.GenerateMock();
_Stub(x=>x.yourdset.PropertyBehavior();
_context.yourdset=GenerateSet(yourdset);
}
public static IDbSet<T> GenerateSet<T>(IList<T> data) where T : class
{
IQueryable<T> queryable = data.AsQueryable();
IDbSet<T> dbSet = MockRepository.GenerateMock<IDbSet<T>, IQueryable>();
dbSet.Stub(x => x.Provider).Return(queryable.Provider);
dbSet.Stub(x => x.Expression).Return(queryable.Expression);
dbSet.Stub(x => x.ElementType).Return(queryable.ElementType);
dbSet.Stub(x => x.GetEnumerator()).Return(null).WhenCalled(x => queryable.GetEnumerator());
return dbSet;
}
[TestInitialize]
public new void Initialize()
{
base.Initialize();
_context = MockRepository.GenerateMock<YourContextInterface>();
_context.Stub(x => x.YourDbSet).PropertyBehavior();
_context.YourDbSet= GenerateSet(YourDbSet);
}