[Fixes #8250] Improve the error message when TEntryPoint is not in an entry point assembly

Validates that the type used as a generic argument in WebApplicaitonFactory is contained within the entry point to assembly and throws InvalidOperationException otherwise
This commit is contained in:
Casey O'Brien 2018-09-06 21:02:21 -04:00 committed by Javier Calvarro Nelson
parent 863b1c2c97
commit 74ba9898f4
5 changed files with 55 additions and 1 deletions

View File

@ -38,6 +38,20 @@ namespace Microsoft.AspNetCore.Mvc.Testing
internal static string FormatMissingDepsFile(object p0, object p1)
=> string.Format(CultureInfo.CurrentCulture, GetString("MissingDepsFile"), p0, p1);
/// <summary>
/// The provided Type '{0}' does not belong to an assembly with an entry point. A common cause for this error is providing a Type from a class library.
/// </summary>
internal static string InvalidAssemblyEntryPoint
{
get => GetString("InvalidAssemblyEntryPoint");
}
/// <summary>
/// The provided Type '{0}' does not belong to an assembly with an entry point. A common cause for this error is providing a Type from a class library.
/// </summary>
internal static string FormatInvalidAssemblyEntryPoint(string p0)
=> string.Format(CultureInfo.CurrentCulture, GetString("InvalidAssemblyEntryPoint"), p0);
private static string GetString(string name, params string[] formatterNames)
{
var value = _resourceManager.GetString(name);

View File

@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="InvalidAssemblyEntryPoint" xml:space="preserve">
<value>The provided Type '{0}' does not belong to an assembly with an entry point. A common cause for this error is providing a Type from a class library.</value>
</data>
<data name="MissingCreateWebHostBuilderMethod" xml:space="preserve">
<value>No method 'public static {0} CreateWebHostBuilder(string[] args)' found on '{1}'. Alternatively, {2} can be extended and 'protected virtual {0} {3}()' can be overridden to provide your own {0} instance.</value>
</data>

View File

@ -251,6 +251,11 @@ namespace Microsoft.AspNetCore.Mvc.Testing
private void EnsureDepsFile()
{
if (typeof(TEntryPoint).Assembly.EntryPoint == null)
{
throw new InvalidOperationException(Resources.FormatInvalidAssemblyEntryPoint(typeof(TEntryPoint).Name));
}
var depsFileName = $"{typeof(TEntryPoint).Assembly.GetName().Name}.deps.json";
var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName));
if (!depsFile.Exists)

View File

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
@ -11,6 +12,7 @@ using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.Mvc.Testing.Handlers;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using RazorPagesClassLibrary;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
@ -39,6 +41,15 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
Assert.Equal("Test", response);
}
[Fact]
public void TestingInfrastructure_CreateClientThrowsInvalidOperationForNonEntryPoint()
{
var factory = new WebApplicationFactory<ClassLibraryStartup>();
var ex = Assert.Throws<InvalidOperationException>(() => factory.CreateClient());
Assert.Equal($"The provided Type '{typeof(RazorPagesClassLibrary.ClassLibraryStartup).Name}' does not belong to an assembly with an entry point. A common cause for this error is providing a Type from a class library.",
ex.Message);
}
[Fact]
public async Task TestingInfrastructure_RedirectHandlerWorksWithPreserveMethod()
{

View File

@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace RazorPagesClassLibrary
{
/// <summary>
/// Empty Startup for testing a Startup file within a class library
/// </summary>
public class ClassLibraryStartup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app)
{
}
}
}