Many times, we need to send email notifications in our application. However, during the local development stage, we may not have access to SMTP server, therefore the email function would throw errors. The easiest way to solve this problem is to use the built-in function provided by SmtpClient in C# to save the email to specified folder location. using (var smtp = new SmtpClient(_host, _port)) { using (var message = new MailMessage()) { message.Subject = "this is a test"; message.Body = "<p>Test email body</p>"; message.IsBodyHtml = true; message.To.Add(new MailAddress("tester@test.com", "tester")); message.From = new MailAddress("tester1@test.com", tester1); #if DEBUG smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; const string em...
Mock Entity Framework FindAsync() method In entity framework, FindAsync() method has signature of: public virtual ValueTask<TEntity> FindAsync([CanBeNullAttribute] params object[] keyValues); Assume we have a method in DB repository that looks for brand information by brand ID: public async Task<Brand> GetBrandbyIdAsync(int brandId) { var brand = await _context.Brands.FindAsync(brandId).ConfigureAwait(false); return brand; } In order to make this method work, we need to mock the FindAsync call inside the method: var dbSetMock = CreateDbSetMock(brands ); dbSetMock.Setup(m => m.FindAsync(It.IsAny<object[]>())).Returns((object[] r) => ...