PageRenderTime 80ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/hereyes/ninject.extensions.interception
C# | 162 lines | 130 code | 32 blank | 0 comment | 0 complexity | 1811fbd4aadab6971d0279cd121cc262 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 Ninject.Extensions.Interception.Interceptors;
  7. using Xunit;
  8. public abstract class MethodInterceptionContext<TInterceptionModule> : InterceptionTestContext<TInterceptionModule>
  9. where TInterceptionModule : InterceptionModule, new()
  10. {
  11. protected override StandardKernel CreateDefaultInterceptionKernel()
  12. {
  13. StandardKernel kernel = base.CreateDefaultInterceptionKernel();
  14. kernel.Bind<Mock>().ToSelf().WithConstructorArgument("myProperty", "start");
  15. return kernel;
  16. }
  17. [Fact]
  18. public void MethodInterceptedWithReplace()
  19. {
  20. using (StandardKernel kernel = CreateDefaultInterceptionKernel())
  21. {
  22. var mock = kernel.Get<Mock>();
  23. mock.MyProperty.Should().Be("start");
  24. mock.GetMyProperty().Should().Be("start");
  25. }
  26. using (StandardKernel kernel = CreateDefaultInterceptionKernel())
  27. {
  28. kernel.InterceptReplace<Mock>(o => o.GetMyProperty(), i => i.ReturnValue = "intercepted");
  29. var mock = kernel.Get<Mock>();
  30. mock.MyProperty.Should().Be("start");
  31. mock.GetMyProperty().Should().Be("intercepted");
  32. }
  33. }
  34. [Fact]
  35. public void MethodInterceptedBefore()
  36. {
  37. string testString = "empty";
  38. using (StandardKernel kernel = CreateDefaultInterceptionKernel())
  39. {
  40. kernel.InterceptBefore<Mock>(
  41. o => o.SetMyProperty(string.Empty),
  42. i => testString = ((Mock)i.Request.Target).MyProperty);
  43. var mock = kernel.Get<Mock>();
  44. mock.MyProperty.Should().Be("start");
  45. testString.Should().Be("empty");
  46. mock.SetMyProperty("end");
  47. mock.MyProperty.Should().Be("end");
  48. testString.Should().Be("start");
  49. }
  50. }
  51. [Fact]
  52. public void MethodInterceptedAfter()
  53. {
  54. string testString = "empty";
  55. using (StandardKernel kernel = CreateDefaultInterceptionKernel())
  56. {
  57. kernel.InterceptAfter<Mock>(
  58. o => o.SetMyProperty(string.Empty),
  59. i => testString = ((Mock)i.Request.Target).MyProperty);
  60. var mock = kernel.Get<Mock>();
  61. mock.MyProperty.Should().Be("start");
  62. testString.Should().Be("empty");
  63. mock.SetMyProperty("end");
  64. mock.MyProperty.Should().Be("end");
  65. testString.Should().Be("end");
  66. }
  67. }
  68. [Fact]
  69. public void MethodCanBeInterceptedWithAddMethodInterceptor()
  70. {
  71. string testString = "empty";
  72. using (StandardKernel kernel = CreateDefaultInterceptionKernel())
  73. {
  74. kernel.AddMethodInterceptor(typeof(Mock).GetMethod("SetMyProperty"),
  75. i => testString = "intercepted");
  76. var mock = kernel.Get<Mock>();
  77. mock.SetMyProperty( "dummy" );
  78. mock.MyProperty.Should().Be("start");
  79. testString.Should().Be("intercepted");
  80. }
  81. }
  82. [Fact]
  83. public void MethodInterceptedWithAddMethodInterceptorCanBeResumed()
  84. {
  85. string testString = "empty";
  86. using (StandardKernel kernel = CreateDefaultInterceptionKernel())
  87. {
  88. kernel.AddMethodInterceptor( typeof (Mock).GetMethod( "SetMyProperty" ),
  89. i => {
  90. testString = "intercepted";
  91. i.Proceed();
  92. } );
  93. var mock = kernel.Get<Mock>();
  94. mock.SetMyProperty("dummy");
  95. mock.MyProperty.Should().Be("dummy");
  96. testString.Should().Be("intercepted");
  97. }
  98. }
  99. [Fact]
  100. public void CanInterceptMethodsWithRefValue()
  101. {
  102. CountInterceptor.Reset();
  103. using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
  104. {
  105. var binding = kernel.Bind<RefAndOutValues>().ToSelf();
  106. binding.Intercept().With<CountInterceptor>();
  107. var foo = kernel.Get<RefAndOutValues>();
  108. int x = 2;
  109. foo.Add(1, ref x, 2);
  110. CountInterceptor.Count.Should().Be(1);
  111. x.Should().Be(3);
  112. }
  113. }
  114. [Fact]
  115. public void CanInterceptMethodsWithOutValue()
  116. {
  117. CountInterceptor.Reset();
  118. using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
  119. {
  120. var binding = kernel.Bind<RefAndOutValues>().ToSelf();
  121. binding.Intercept().With<CountInterceptor>();
  122. var foo = kernel.Get<RefAndOutValues>();
  123. int x;
  124. foo.Multiply(2, out x, 3);
  125. CountInterceptor.Count.Should().Be(1);
  126. x.Should().Be(6);
  127. }
  128. }
  129. }
  130. }