BaseTests.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Masuit.Tools.Test.Mvc.Mocks;
  2. using Moq;
  3. using NUnit.Framework;
  4. using System;
  5. using System.IO;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Web.Routing;
  9. namespace Masuit.Tools.Test.Mvc
  10. {
  11. [TestFixture]
  12. public abstract class BaseTests
  13. {
  14. protected internal MockHttpResponse Response { get; set; }
  15. protected internal HttpContextBase Context { get; set; }
  16. protected internal MockHttpRequest Request { get; set; }
  17. protected internal MockHttpSession Session { get; private set; }
  18. protected string TestDirectoryPath()
  19. {
  20. return new DirectoryInfo(AppContext.BaseDirectory + ".\\Resources").FullName;
  21. }
  22. protected FileInfo TestFile(string fileName)
  23. {
  24. return new FileInfo($"{TestDirectoryPath()}\\{fileName}");
  25. }
  26. protected string FilePath(string fileName)
  27. {
  28. return TestFile(fileName).FullName;
  29. }
  30. protected BaseTests()
  31. {
  32. InitMocks();
  33. }
  34. [SetUp]
  35. public void BaseTestsSetup()
  36. {
  37. InitMocks();
  38. }
  39. protected void InitMocks()
  40. {
  41. var mockHttpContext = new Mock<HttpContextBase>();
  42. Context = mockHttpContext.Object;
  43. Session = new MockHttpSession();
  44. Request = new MockHttpRequest(new MockHttpFilesCollection(null));
  45. Response = new MockHttpResponse();
  46. mockHttpContext.Setup(ctx => ctx.Session).Returns(() => Session);
  47. mockHttpContext.Setup(ctx => ctx.Request).Returns(() => Request);
  48. mockHttpContext.Setup(ctx => ctx.Response).Returns(() => Response);
  49. mockHttpContext.Setup(ctx => ctx.Cache).Returns(() => HttpRuntime.Cache);
  50. }
  51. protected ControllerContext ControllerContext<T>(T controller) where T : ControllerBase
  52. {
  53. return new ControllerContext(Context, new RouteData(), controller);
  54. }
  55. protected ModelBindingContext BindingContext<T>()
  56. {
  57. return new ModelBindingContext
  58. {
  59. ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(T))
  60. };
  61. }
  62. protected T MockObject<T>() where T : class
  63. {
  64. var mock = new Mock<T>();
  65. return mock.Object;
  66. }
  67. protected Mock<T> Mock<T>() where T : class
  68. {
  69. return new Mock<T>();
  70. }
  71. }
  72. }