PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Ninject.Extensions.Interception.Test/PropertyInterceptionContext.cs

https://github.com/hereyes/ninject.extensions.interception
C# | 113 lines | 91 code | 22 blank | 0 comment | 2 complexity | 3bee8322f39400756830ef595ed250e8 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. namespace Ninject.Extensions.Interception
  2. {
  3. using FluentAssertions;
  4. using Ninject.Extensions.Interception.Fakes;
  5. using Ninject.Extensions.Interception.Infrastructure.Language;
  6. using Xunit;
  7. public abstract class PropertyInterceptionContext<TInterceptionModule> :
  8. InterceptionTestContext<TInterceptionModule> where TInterceptionModule : InterceptionModule, new()
  9. {
  10. protected override StandardKernel CreateDefaultInterceptionKernel()
  11. {
  12. StandardKernel kernel = base.CreateDefaultInterceptionKernel();
  13. kernel.Bind<Mock>().ToSelf().WithConstructorArgument("myProperty", "start");
  14. return kernel;
  15. }
  16. [Fact]
  17. public void PropertyGetInterceptedWithReplace()
  18. {
  19. using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
  20. {
  21. var obj = kernel.Get<Mock>();
  22. obj.GetMyProperty().Should().Be("start");
  23. obj.MyProperty.Should().Be("start");
  24. }
  25. using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
  26. {
  27. kernel.InterceptReplaceGet<Mock>(
  28. o => o.MyProperty,
  29. i => i.ReturnValue = "intercepted");
  30. var obj = kernel.Get<Mock>();
  31. obj.GetMyProperty().Should().Be("start");
  32. obj.MyProperty.Should().Be("intercepted");
  33. }
  34. }
  35. [Fact]
  36. public void PropertySetInterceptedWithReplace()
  37. {
  38. using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
  39. {
  40. var obj = kernel.Get<Mock>();
  41. obj.MyProperty.Should().Be("start");
  42. obj.MyProperty = "end";
  43. obj.MyProperty.Should().Be("end");
  44. }
  45. using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
  46. {
  47. kernel.InterceptReplaceSet<Mock>(o => o.MyProperty, i => { });
  48. var obj = kernel.Get<Mock>();
  49. obj.MyProperty.Should().Be("start");
  50. obj.MyProperty = "end";
  51. obj.MyProperty.Should().Be("start");
  52. }
  53. }
  54. [Fact]
  55. public void PropertyGetInterceptedBefore()
  56. {
  57. string testString = "empty";
  58. using ( StandardKernel kernel = CreateDefaultInterceptionKernel() )
  59. {
  60. kernel.InterceptBeforeGet<Mock>(
  61. o => o.MyProperty,
  62. i =>
  63. {
  64. if (i.ReturnValue == null)
  65. {
  66. testString = "null";
  67. }
  68. });
  69. var obj = kernel.Get<Mock>();
  70. testString.Should().Be("empty");
  71. obj.MyProperty.Should().Be("start");
  72. testString.Should().Be("null");
  73. }
  74. }
  75. [Fact]
  76. public void PropertyGetInterceptedAfter()
  77. {
  78. string testString = "empty";
  79. using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
  80. {
  81. kernel.InterceptAfterGet<Mock>(
  82. o => o.MyProperty,
  83. i => testString = i.ReturnValue.ToString());
  84. var obj = kernel.Get<Mock>();
  85. testString.Should().Be("empty");
  86. obj.MyProperty.Should().Be("start");
  87. testString.Should().Be("start");
  88. }
  89. }
  90. }
  91. }