簡體   English   中英

未知命令:--environment=Development during testing

[英]Unknown command: --environment=Development during testing

我創建了一個 ASP.NET 項目並為它編寫了一些集成測試。 但是當我嘗試運行dotnet test時,會出現:

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
The active test run was aborted. Reason: Test host process crashed : Unknown command: --environment=Development


Test Run Aborted with error System.Exception: One or more errors occurred.
 ---> System.Exception: Unable to read beyond the end of the stream.
   at System.IO.BinaryReader.Read7BitEncodedInt()
   at System.IO.BinaryReader.ReadString()
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.LengthPrefixCommunicationChannel.NotifyDataAvailable()
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TcpClientExtensions.MessageLoopAsync(TcpClient client, ICommunicationChannel channel, Action`1 errorHandler, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---.

據我了解,有些東西試圖用--environment=Development運行 dotnet 可執行文件,但即使在Microsoft docs中使用了這個參數,它也是無效的。

我嘗試創建新的 ASP.NET 項目(沒有控制器、服務、數據庫等。只是 API 什么都不做,並且測試為空),但我無法再次重現該錯誤。

最初我偶然在同一個文件夾中創建了我的項目和解決方案,並且不得不手動將項目移動到子文件夾。 在我這樣做之后一切都很好,所以我認為它很好。 也許這就是原因。

以下是我在測試期間訪問應用程序的方式:


// TestingApplication.cs
public class TestingApplication : WebApplicationFactory<Program>
{
    private readonly Guid _appId = Guid.NewGuid();

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        // Add mock/test services to the builder here
        builder.ConfigureServices(services =>
        {
            services.AddMvcCore().AddApplicationPart(typeof(Program).Assembly);
            services.AddScoped(sp => new DbContextOptionsBuilder<EfDbContext>()
                .UseSqlServer(
                    $"DATABASE CONNECTION STRING")
                .UseApplicationServiceProvider(sp)
                .Options);
        });
    }

    protected override IHost CreateHost(IHostBuilder builder)
    {
        var host = base.CreateHost(builder);

        using (var serviceScope = host.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetRequiredService<EfDbContext>();
            context.Database.EnsureCreated();
        }

        return host;
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        
        using (var serviceScope = Server.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetRequiredService<EfDbContext>();
            context.Database.EnsureDeleted();
        }
    }
}

// BaseTest.cs
public class BaseTest : IDisposable, IClassFixture<TestingApplication>
{
    protected readonly TestingApplication Application;
    private HttpClient? _client;

    protected HttpClient Client => _client ??= Application.CreateClient();
    
    public BaseTest(TestingApplication testingApplication)
    {
        Application = testingApplication;
    }

    public void Dispose()
    {
        Application.Dispose();
    }
}

更多信息:

  • 單元測試工作得很好
  • 最初我忘記將<InternalsVisibleTo Include="NameOfTestsProject" />添加到主項目文件中,但無論如何它都不起作用。
  • .NET 6、操作系統 - Linux、IDE - Jetbrains Rider
  • 重建解決方案不起作用
  • 為單元測試創​​建新項目也無濟於事

有誰知道問題是什么?

UPD我想通了

好的,所以這只是復制粘貼別人的代碼而不檢查的另一個例子。 我復制了以下內容:

if (args[0] == "something") {
 ...
} else if (args[0] == "something else") {
 ...
} else {
 // exit with code 1 here and print error
}

在我的 Program.cs 中。 它本身運行良好,但在測試時導致了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM