[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:
parent
863b1c2c97
commit
74ba9898f4
|
|
@ -38,6 +38,20 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
||||||
internal static string FormatMissingDepsFile(object p0, object p1)
|
internal static string FormatMissingDepsFile(object p0, object p1)
|
||||||
=> string.Format(CultureInfo.CurrentCulture, GetString("MissingDepsFile"), p0, 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)
|
private static string GetString(string name, params string[] formatterNames)
|
||||||
{
|
{
|
||||||
var value = _resourceManager.GetString(name);
|
var value = _resourceManager.GetString(name);
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,9 @@
|
||||||
<resheader name="writer">
|
<resheader name="writer">
|
||||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
</resheader>
|
</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">
|
<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>
|
<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>
|
</data>
|
||||||
|
|
|
||||||
|
|
@ -251,6 +251,11 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
||||||
|
|
||||||
private void EnsureDepsFile()
|
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 depsFileName = $"{typeof(TEntryPoint).Assembly.GetName().Name}.deps.json";
|
||||||
var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName));
|
var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName));
|
||||||
if (!depsFile.Exists)
|
if (!depsFile.Exists)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Linq;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Formatting;
|
using System.Net.Http.Formatting;
|
||||||
|
|
@ -11,6 +12,7 @@ using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
using Microsoft.AspNetCore.Mvc.Testing.Handlers;
|
using Microsoft.AspNetCore.Mvc.Testing.Handlers;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using RazorPagesClassLibrary;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
|
|
@ -39,6 +41,15 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
Assert.Equal("Test", response);
|
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]
|
[Fact]
|
||||||
public async Task TestingInfrastructure_RedirectHandlerWorksWithPreserveMethod()
|
public async Task TestingInfrastructure_RedirectHandlerWorksWithPreserveMethod()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue