Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using Xunit;
  4. public class EmailTemplateUnitTest
  5. {
  6. [Fact]
  7. public void GetEmailTemplate_Repository_Layer()
  8. {
  9.  
  10. var emailTemplate = new EmailTemplate();
  11. var context = GetDbContext();
  12. var result = new EmailTemplateRepository(context).getEmailTemplateByName("forgotpassword");
  13. var emailTemplateId = result.Result.EmailTemplateId;
  14. Assert.Equal(1, result.Result.EmailTemplateId);
  15. }
  16.  
  17.  
  18. public NotificationContext GetDbContext()
  19. {
  20. var builder = new DbContextOptionsBuilder<NotificationContext>();
  21. builder.UseSqlServer("Data Source=solodev.database.windows.net;
  22. Initial Catalog=DatabaseName;User ID=user; Password=Password");
  23. var dbContext = new NotificationContext(builder.Options);
  24. dbContext.Database.EnsureCreated();
  25. return dbContext;
  26. }
  27. }
  28.  
  29. public class EmailTemplateRepository : GenericRepository<EmailTemplate>, IEmailTemplateRepository
  30. {
  31. private readonly NotificationContext _notificationContext;
  32. public EmailTemplateRepository(NotificationContext repositoryContext)
  33. : base(repositoryContext)
  34. {
  35. _notificationContext = repositoryContext;
  36. }
  37. public async Task<EmailTemplate> getEmailTemplateByName(string Name)
  38. {
  39. try
  40. {
  41. var templateDate = await FindAsync(e => e.MailFor == Name);
  42. return templateDate;
  43. }
  44. catch (Exception ex)
  45. {
  46. throw;
  47. }
  48.  
  49. }
  50. }
  51.  
  52. public class GenericRepository<T> : IGenericRepository<T> where T : class
  53. {
  54. protected GenericDataContext _context;
  55. public virtual async Task<T> FindAsync(Expression<Func<T, bool>> match)
  56. {
  57. try
  58. {
  59. return await _context.Set<T>().SingleOrDefaultAsync(match);
  60. }
  61. catch (Exception ex)
  62. {
  63.  
  64. throw;
  65. }
  66.  
  67. }
  68. }
  69.  
  70. public class GenericDataContext : DbContext
  71. {
  72. public GenericDataContext(DbContextOptions<GenericDataContext> options) : base(options) { }
  73.  
  74. protected GenericDataContext(DbContextOptions options) : base(options) { }
  75.  
  76. }
  77.  
  78. public class EmailTemplateUnitTest
  79. {
  80.  
  81. [Fact]
  82. public void GetEmailTemplate_Service_Layer_By_Mock()
  83. {
  84. var emailTemplate = new EmailTemplate();
  85. var productRepositoryMock = new Mock<IGenericRepository<EmailTemplate>>();
  86. var unitOfWorkMock = new Mock<EmailTemplateRepository>(GetDbContext());
  87. IEmailTemplateService sut = new EmailTemplateService(unitOfWorkMock.Object);
  88.  
  89. var actual = sut.getEmailTemplateByName("forgotpassword");
  90. Assert.NotNull(actual);//assert that a result was returned
  91. Assert.Equal(1, actual.Result.ResponseInfo.EmailTemplateId);
  92.  
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement