How can I use UseEmbeddedResourcesProject in my unit test project? #418
-
I have
It should not be looking in My registration method: /// <summary>
/// Sets up FluentEmail and RazorLight for email templates.
/// </summary>
/// <param name="services">The IServiceCollection.</param>
/// <param name="hostingEnvironment">The IHostEnvironment.</param>
/// <param name="sendGridOptions">The SendGridOptions.</param>
/// <param name="embeddedResRootType">The root type for the project that has the embedded templates..</param>
/// <returns>The same IServiceCollection.</returns>
public static IServiceCollection AddEmail(
this IServiceCollection services,
IHostEnvironment hostingEnvironment,
SendGridOptions sendGridOptions,
Type embeddedResRootType) =>
services.AddFluentEmail(sendGridOptions.EmailSender)
.AddRazorRenderer(embeddedResRootType)
.Services
.AddIfElse(
hostingEnvironment.IsDevelopment(),
devServices => devServices.AddSingleton<ISender>(x => new SmtpSender(new SmtpClient("localhost", 25))),
prodServices => prodServices.AddSingleton<ISender>(x => new SendGridSender(sendGridOptions.APIKey))); My test looks similar to this: [Fact]
public void Should_Send_Email_and_Create_Output()
{
var actualEmailSendTo = string.Empty;
var actualEmailSendFrom = string.Empty;
var actualSubject = string.Empty;
// Mock the sender
var mockEmailSender = new Mock<ISender>();
mockEmailSender
.Setup(x => x.SendAsync(It.IsAny<IFluentEmail>(), null))
.Callback((IFluentEmail email, CancellationToken? token) =>
{
actualEmailSendTo = email.Data.ToAddresses.Single().EmailAddress;
actualEmailSendFrom = email.Data.FromAddress.EmailAddress;
actualSubject = email.Data.Subject;
});
var factory = new CustomWebApplicationFactory<Program>(this.testOutputHelper)
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
services.AddSingleton(mockEmailSender.Object);
});
});
// Start the server
factory.CreateClient(); I show the above to show that CustomWebAppFactory is what launches the host and all that. So maybe thats part of the issue? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Did you look at our tests? There is also the new Debug options added in rc3 as part of the RazorLightEngineBuilder. Please try enabling that first. |
Beta Was this translation helpful? Give feedback.
-
Turns out in Fluent.Email, I was not using |
Beta Was this translation helpful? Give feedback.
Turns out in Fluent.Email, I was not using
.UsingTemplate
when I injected IFluentEmail into my service. So internally, it was using something other than my razor engine.