如何使用犀牛制品存根ninject的的iKernel?

var mocks = new MockRepository(); var access = new Access(); access.ShowRepository = _mocks.Stub<IShowRepository>(); access.ShowRepository.Stub(x => x.GetShows()).Return(new List<Show>()); var kernel =_mocks.Stub<IKernel>(); kernel.Stub(x => x.Get<Access>()).Return(access);

这引发ArgumentNullException:

消息:值不能为空。
参数名:源

堆栈跟踪:在System.Linq.Enumerable.Cast [TResult](IEnumerable的源)在Ninject.ResolutionExtensions.Get [T](IResolutionRoot根,IParameter []参数)在MediaMover.Unit.Tests.ViewModelTests.OptionsViewModelTests.b__1(的iKernel X)在C:\用户\戴夫\文档\ Visual Studio 2010的\项目\ Gigatube \ MediaMover.Unit.Tests \ ViewModelTests \ OptionsViewModelTests.cs:行33在Rhino.Mocks.RhinoMocksExtensions.Expect [T,R](T模拟,功能2 action) at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function 2动作)在MediaMover.Unit.Tests.ViewModelTests.OptionsViewModelTests.Initialize()在C:\用户\戴夫\文件\ Visual Studio 2010的\项目\ Gigatube \ MediaMover.Unit.Tests \ ViewModelTests \ OptionsViewModelTests.cs:行33

--------------解决方案-------------

我能解决这个问题,我不得不创建一个测试模块和测试供应商,​​并通过那些通过我的嘲笑。

class DbAccessProvider : Provider<Access>
{
public Access DbAccess { get; set; }
protected override Access CreateInstance(IContext context)
{
return DbAccess;
}
}

class TestModule : NinjectModule
{
public DbAccessProvider DbAccessProvider { get; set; }
public override void Load()
{
Bind<Access>().ToProvider(DbAccessProvider);
}
}

下面是我的测试我的设置:

access = new Access();
access.SettingsRepository = MockRepository.GenerateStub<ISettingsRepository>();
access.ShowRepository = MockRepository.GenerateStub<IShowRepository>();
access.SettingsRepository.Stub(x => x.GetById(1)).Return(_settings);
DbAccessProvider dbAccessProvider = new DbAccessProvider();
dbAccessProvider.DbAccess = access;
TestModule testModule = new TestModule();
testModule.DbAccessProvider = dbAccessProvider;
var kernel = new StandardKernel(testModule);
target = new OptionsViewModel(kernel);

分类:C# 时间:2015-03-15 人气:0
本文关键词: C#,犀牛嘲笑,ninject
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.242 (s). 10 q(s)