Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. public class SomeClassFromDomainLayer
  2. {
  3. //some code
  4. #region private properties
  5. private IContext Context
  6. {
  7. get
  8. {
  9. if(_context==null)
  10. {
  11. _context = AppEnvironment.Kernel.Get<IContext>();
  12. }
  13. return _context;
  14. }
  15. }
  16. #endregion
  17. //some code
  18. }
  19.  
  20. public class ContextNinjectModule : NinjectModule
  21. {
  22. public override void Load()
  23. {
  24. this.Bind<IContext>().To<RealContext>();
  25. }
  26. }
  27.  
  28. public class TestContextNinjectModule : NinjectModule
  29. {
  30. public override void Load()
  31. {
  32. // делегат в ToMethod принимает параметр типа IContext -- это тип NInject'а,
  33. // не путайте с вашим собственным интерфейсом!
  34. this.Bind<IContext>().ToMethod(c => Substitute.For<IContext>());
  35. }
  36. }
  37.  
  38. public void SomeTest
  39. {
  40. var context = module.Get<IContext>();
  41. context.SomeMethod().Returns(42);
  42.  
  43. var classUnderTest = new ClassUnderTest(context);
  44. classUnderTest.Foo();
  45.  
  46. context.Received().SomeMethod();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement