Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.Threading.Tasks;
- using Xunit;
- public class EmailTemplateUnitTest
- {
- [Fact]
- public void GetEmailTemplate_Repository_Layer()
- {
- var emailTemplate = new EmailTemplate();
- var context = GetDbContext();
- var result = new EmailTemplateRepository(context).getEmailTemplateByName("forgotpassword");
- var emailTemplateId = result.Result.EmailTemplateId;
- Assert.Equal(1, result.Result.EmailTemplateId);
- }
- public NotificationContext GetDbContext()
- {
- var builder = new DbContextOptionsBuilder<NotificationContext>();
- builder.UseSqlServer("Data Source=solodev.database.windows.net;
- Initial Catalog=DatabaseName;User ID=user; Password=Password");
- var dbContext = new NotificationContext(builder.Options);
- dbContext.Database.EnsureCreated();
- return dbContext;
- }
- }
- public class EmailTemplateRepository : GenericRepository<EmailTemplate>, IEmailTemplateRepository
- {
- private readonly NotificationContext _notificationContext;
- public EmailTemplateRepository(NotificationContext repositoryContext)
- : base(repositoryContext)
- {
- _notificationContext = repositoryContext;
- }
- public async Task<EmailTemplate> getEmailTemplateByName(string Name)
- {
- try
- {
- var templateDate = await FindAsync(e => e.MailFor == Name);
- return templateDate;
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- }
- public class GenericRepository<T> : IGenericRepository<T> where T : class
- {
- protected GenericDataContext _context;
- public virtual async Task<T> FindAsync(Expression<Func<T, bool>> match)
- {
- try
- {
- return await _context.Set<T>().SingleOrDefaultAsync(match);
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- }
- public class GenericDataContext : DbContext
- {
- public GenericDataContext(DbContextOptions<GenericDataContext> options) : base(options) { }
- protected GenericDataContext(DbContextOptions options) : base(options) { }
- }
- public class EmailTemplateUnitTest
- {
- [Fact]
- public void GetEmailTemplate_Service_Layer_By_Mock()
- {
- var emailTemplate = new EmailTemplate();
- var productRepositoryMock = new Mock<IGenericRepository<EmailTemplate>>();
- var unitOfWorkMock = new Mock<EmailTemplateRepository>(GetDbContext());
- IEmailTemplateService sut = new EmailTemplateService(unitOfWorkMock.Object);
- var actual = sut.getEmailTemplateByName("forgotpassword");
- Assert.NotNull(actual);//assert that a result was returned
- Assert.Equal(1, actual.Result.ResponseInfo.EmailTemplateId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement