WebApplicationFactory找不到模块ClientApp \ dist \ main-server

时间:2018-07-04 16:37:38

标签: reactjs asp.net-core-mvc integration-testing asp.net-core-2.1

我正在尝试为运行React + Redux的.NET-Core应用创建集成测试。当我尝试在我的React + Redux应用程序以外的任何页面上运行单元测试时,它运行正常。但是,在React + Redux页面上,我得到:

Message: Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException : Prerendering failed because of error: Error: Cannot find module 'C:\vso\reward_cards_net\GiftCardWebAPI\Test.GiftCardWebApi.Integration\ClientApp\dist\main-server'
  at Function.Module._resolveFilename (module.js:538:15)
  at Function.Module._load (module.js:468:25)
  at Module.require (module.js:587:17)
  at require (internal/module.js:11:18)
  at findBootModule (C:\Users\Tyler.Findlay\AppData\Local\Temp\pilpilmw.1r0:111:17)
  at findRenderToStringFunc (C:\Users\Tyler.Findlay\AppData\Local\Temp\pilpilmw.1r0:116:28)
  at renderToStringImpl (C:\Users\Tyler.Findlay\AppData\Local\Temp\pilpilmw.1r0:75:51)
  at C:\Users\Tyler.Findlay\AppData\Local\Temp\scedvoik.xdb:114:19
  at IncomingMessage.<anonymous> (C:\Users\Tyler.Findlay\AppData\Local\Temp\scedvoik.xdb:133:38)
  at emitNone (events.js:106:13)
Current directory is: C:\vso\reward_cards_net\GiftCardWebAPI\Test.GiftCardWebApi.Integration

但是,在我的WebApplicationFactory中,我用以下代码覆盖了CreateWebHostBuilder:

public class MockRepositoriesApplicationFactory : WebApplicationFactory<TestStartup>
{
    /// <summary>
    ///     The mock for the applications <see cref="IGiftCardRepository" />
    /// </summary>
    public Mock<GiftCardRepository> GiftCardRepositoryMock;

    /// <summary>
    ///     Creates a new instance of this factory
    /// </summary>
    public MockRepositoriesApplicationFactory()
    {
        GiftCardRepositoryMock = new Mock<GiftCardRepository>();
    }

    /// <inheritdoc />
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        base.ConfigureWebHost(builder);

        builder.ConfigureServices(
            services =>
            {
                GiftCardRepositoryMock = new Mock<GiftCardRepository>();
                services.AddTransient<IGiftCardRepository, GiftCardRepository>(
                    s =>
                        GiftCardRepositoryMock.Object);
            });
    }

    /// <inheritdoc />
    protected override IWebHostBuilder CreateWebHostBuilder()
    {
        return WebHost.CreateDefaultBuilder()
            .UseContentRoot("C:\\vso\\reward_cards_net\\GiftCardWebAPI\\GiftCardWebAPI\\")
            .UseStartup<TestStartup>()
            .UseEnvironment("Staging");
    }
}

单元测试如下:

/// <summary>
///     Integration tests
/// </summary>
public class DashboardUiTests : IClassFixture<MockRepositoriesApplicationFactory>
{
    /// <summary>
    ///     Creates a new instance of this test class
    /// </summary>
    /// <param name="factory"></param>
    public DashboardUiTests(MockRepositoriesApplicationFactory factory)
    {
        _factory = factory;
    }

    /// <summary>
    ///     The web application factory
    /// </summary>
    private readonly MockRepositoriesApplicationFactory _factory;

    /// <summary>
    ///     Tests that the get-card-info page returns the results when the search was successful
    /// </summary>
    /// <returns>An awaitable <see cref="Task" /></returns>
    [Fact]
    public async Task GetCardInfo_ReturnsResults_WhenFound()
    {
        // Arrange
        HttpClient client = _factory.CreateClient();
        client.BaseAddress = new Uri("https://localhost/");

        // Act
        HttpResponseMessage response = await client.GetAsync("/dashboard/get-card-info"); // I get an error on this line

        // Assert
        response.EnsureSuccessStatusCode(); // Status Code 200-299
    }
}

谁能看到我要去哪里错了?

0 个答案:

没有答案