MESInterfaceTestBase.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Microsoft.Extensions.DependencyInjection;
  2. using System;
  3. using System.Threading.Tasks;
  4. using Volo.Abp;
  5. using Volo.Abp.Modularity;
  6. using Volo.Abp.Testing;
  7. using Volo.Abp.Uow;
  8. namespace OrBit.MESInterface.TestBase
  9. {
  10. public abstract class MESInterfaceTestBase<TStartupmodule> : AbpIntegratedTest<TStartupmodule> where TStartupmodule : IAbpModule
  11. {
  12. protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
  13. {
  14. options.UseAutofac();
  15. }
  16. protected virtual async Task WithUnitOfWorkAsync(AbpUnitOfWorkOptions options, Func<Task> action)
  17. {
  18. using (var scope = ServiceProvider.CreateScope())
  19. {
  20. var uomManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
  21. using (var uow = uomManager.Begin(options))
  22. {
  23. await action();
  24. await uow.CompleteAsync();
  25. }
  26. }
  27. }
  28. protected virtual async Task<TResult> WithUnitOfWorkAsync<TResult>(AbpUnitOfWorkOptions options, Func<Task<TResult>> func)
  29. {
  30. using (var scope = ServiceProvider.CreateScope())
  31. {
  32. var uomManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
  33. using (var uow = uomManager.Begin(options))
  34. {
  35. var result = await func();
  36. await uow.CompleteAsync();
  37. return result;
  38. }
  39. }
  40. }
  41. protected virtual Task WithUnitOfWorkAsync(Func<Task> func)
  42. {
  43. return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func);
  44. }
  45. protected virtual Task<TResult> WithUnitOfWorkAsync<TResult>(Func<Task<TResult>> func)
  46. {
  47. return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func);
  48. }
  49. }
  50. }