ExamTestModule.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using Castle.MicroKernel.Registration;
  3. using NSubstitute;
  4. using Abp.AutoMapper;
  5. using Abp.Dependency;
  6. using Abp.Modules;
  7. using Abp.Configuration.Startup;
  8. using Abp.Net.Mail;
  9. using Abp.TestBase;
  10. using Abp.Zero.Configuration;
  11. using Abp.Zero.EntityFrameworkCore;
  12. using YGNT.Exam.EntityFrameworkCore;
  13. using YGNT.Exam.Tests.DependencyInjection;
  14. namespace YGNT.Exam.Tests
  15. {
  16. [DependsOn(
  17. typeof(ExamApplicationModule),
  18. typeof(ExamEntityFrameworkModule),
  19. typeof(AbpTestBaseModule)
  20. )]
  21. public class ExamTestModule : AbpModule
  22. {
  23. public ExamTestModule(ExamEntityFrameworkModule abpProjectNameEntityFrameworkModule)
  24. {
  25. abpProjectNameEntityFrameworkModule.SkipDbContextRegistration = true;
  26. abpProjectNameEntityFrameworkModule.SkipDbSeed = true;
  27. }
  28. public override void PreInitialize()
  29. {
  30. Configuration.UnitOfWork.Timeout = TimeSpan.FromMinutes(30);
  31. Configuration.UnitOfWork.IsTransactional = false;
  32. // Disable static mapper usage since it breaks unit tests (see https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2052)
  33. Configuration.Modules.AbpAutoMapper().UseStaticMapper = false;
  34. Configuration.BackgroundJobs.IsJobExecutionEnabled = false;
  35. // Use database for language management
  36. Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
  37. RegisterFakeService<AbpZeroDbMigrator<ExamDbContext>>();
  38. Configuration.ReplaceService<IEmailSender, NullEmailSender>(DependencyLifeStyle.Transient);
  39. }
  40. public override void Initialize()
  41. {
  42. ServiceCollectionRegistrar.Register(IocManager);
  43. }
  44. private void RegisterFakeService<TService>() where TService : class
  45. {
  46. IocManager.IocContainer.Register(
  47. Component.For<TService>()
  48. .UsingFactoryMethod(() => Substitute.For<TService>())
  49. .LifestyleSingleton()
  50. );
  51. }
  52. }
  53. }