Tutorial :How to stub ninject's IKernel using Rhino Mocks?


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);  

This throws an ArgumentNullException:

Message: Value cannot be null.
Parameter name: source

Stack Trace: at System.Linq.Enumerable.Cast[TResult](IEnumerable source) at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters) at MediaMover.Unit.Tests.ViewModelTests.OptionsViewModelTests.b__1(IKernel x) in C:\Users\Dave\Documents\Visual Studio 2010\Projects\Gigatube\MediaMover.Unit.Tests\ViewModelTests\OptionsViewModelTests.cs:line 33 at Rhino.Mocks.RhinoMocksExtensions.Expect[T,R](T mock, Function2 action) at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function2 action) at MediaMover.Unit.Tests.ViewModelTests.OptionsViewModelTests.Initialize() in C:\Users\Dave\Documents\Visual Studio 2010\Projects\Gigatube\MediaMover.Unit.Tests\ViewModelTests\OptionsViewModelTests.cs:line 33


I was able to solve this, I had to create a test module and a test provider and pass my mocks through those.

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);          }      }  

Here is my SetUp from my test:

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);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »