Skip to content

Commit

Permalink
chore: fix test path
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanho committed Mar 21, 2024
1 parent a478d79 commit c21e5a6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
15 changes: 13 additions & 2 deletions src/Postal.AspNetCore/TemplateServices/TemplateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Postal.AspNetCore
Expand Down Expand Up @@ -125,13 +126,23 @@ public async Task<string> RenderTemplateAsync<TViewModel>(RouteData routeData,

if ((viewResult == null || !viewResult.Success) && (razorPageResult == null || (razorPageResult != null && razorPageResult?.Page == null)))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Failed to render template {viewName} because it was not found.");
var searchedLocations = viewResult?.SearchedLocations ?? [];
if (razorPageResult != null && razorPageResult?.SearchedLocations != null)
{
searchedLocations = searchedLocations.Union(razorPageResult?.SearchedLocations!);
}
_logger.LogError($"Failed to render template {viewName} because it was not found. \r\nThe following locations are searched: \r\n{string.Join("\r\n", searchedLocations)}");
throw new TemplateServiceException($"Failed to render template {viewName} because it was not found. \r\nThe following locations are searched: \r\n{string.Join("\r\n", searchedLocations)}");
if (_hostingEnvironment.ContentRootPath != null
&& File.Exists(Path.Combine(_hostingEnvironment.ContentRootPath, viewName))
&& !IsApplicationRelativePath(viewName))
{
sb.AppendLine($"ViewName requires \"~/\" prefix. Try change the viewName to \"~/{viewName.TrimStart('/')}\"");
}
sb.AppendLine("The following locations are searched:");
sb.AppendLine(string.Join("\r\n", searchedLocations));
_logger.LogError(sb.ToString());
throw new TemplateServiceException(sb.ToString());
}

var viewDictionary = new ViewDataDictionary<TViewModel>(new EmptyModelMetadataProvider(), new ModelStateDictionary());
Expand Down
2 changes: 2 additions & 0 deletions src/Postal.Tests/EmailViewRenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public async Task Render_throws_exception_when_email_view_not_found()
var serviceProvider = new Mock<IServiceProvider>();
var tempDataProvider = new Mock<ITempDataProvider>();
var hostingEnvironment = new Mock<IWebHostEnvironment>();
hostingEnvironment.SetupGet(e => e.ContentRootPath).Returns("");
var diagnosticListener = new System.Diagnostics.DiagnosticListener("Postal.Tests");
var urlHelperFactory = new Mock<IUrlHelperFactory>();
var options = new Mock<Microsoft.Extensions.Options.IOptions<EmailServiceOptions>>();
Expand Down Expand Up @@ -165,6 +166,7 @@ public async Task Render_throws_exception_when_email_view_retrievepath_not_found
var serviceProvider = new Mock<IServiceProvider>();
var tempDataProvider = new Mock<ITempDataProvider>();
var hostingEnvironment = new Mock<IWebHostEnvironment>();
hostingEnvironment.SetupGet(e => e.ContentRootPath).Returns("");
var diagnosticListener = new System.Diagnostics.DiagnosticListener("Postal.Tests");
var urlHelperFactory = new Mock<IUrlHelperFactory>();
var options = new Mock<Microsoft.Extensions.Options.IOptions<EmailServiceOptions>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public IActionResult SendEmail1()

public IActionResult SendEmail2()
{
var emailData = new Email("Pages/Emails/Testing2.cshtml");
var emailData = new Email("~/Pages/Emails/Testing2.cshtml");
emailData.ViewData["to"] = "[email protected]";
emailData.CaptureHttpContext(HttpContext);

Expand Down
3 changes: 1 addition & 2 deletions test/Postal.Tests.Integration/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class CustomWebApplicationFactory<TProgram>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
//builder.UseContentRoot(AppContext.BaseDirectory);
builder.UseSolutionRelativeContentRoot("test/Postal.Tests.Integration");
builder.UseSolutionRelativeContentRoot("test/Postal.Tests.Integration/");
}
}
36 changes: 23 additions & 13 deletions test/Postal.Tests.Integration/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:62985",
"sslPort": 0
}
},
{
"profiles": {
"Postal.Tests.Integration": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5208",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5208"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WSL": {
"commandName": "WSL2",
"launchBrowser": true,
"launchUrl": "http://localhost:5208",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:5208"
},
"distributionName": ""
}
},
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:62985",
"sslPort": 0
}
}
}
}

0 comments on commit c21e5a6

Please sign in to comment.