首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未知命令:-环境=测试期间的开发

未知命令:-环境=测试期间的开发
EN

Stack Overflow用户
提问于 2022-07-07 10:46:20
回答 1查看 141关注 0票数 0

我创建了一个ASP.NET项目,并为它编写了一些集成测试。但是当我试图运行dotnet test时,这个结果就会显示出来:

代码语言:javascript
运行
复制
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可执行文件,但是即使在微软文档中使用它,这个参数也是无效的。

我尝试创建新的ASP.NET项目(没有控制器、服务、数据库等,只是什么都不做、测试是空的),但我无法再次重现错误。

最初,我偶然地在同一个文件夹中创建了我的项目和解决方案,并且不得不手动将项目移动到子文件夹。我这么做后一切都很好,所以我认为一切都很好。也许这就是原因。

下面是我在测试期间访问应用程序的方式:

代码语言:javascript
运行
复制
// 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,OS - Linux,IDE - Jetbrains Rider
  • 重建解决方案无效
  • 为单元测试创建新项目也没有帮助。

有人知道问题出在哪里吗?

UPD我想通了

EN

回答 1

Stack Overflow用户

发布于 2022-07-07 12:15:44

好的,这只是复制粘贴别人的代码而不检查的又一个例子。我抄袭了一些东西,大意是:

代码语言:javascript
运行
复制
if (args[0] == "something") {
 ...
} else if (args[0] == "something else") {
 ...
} else {
 // exit with code 1 here and print error
}

在我的Program.cs里。它本身工作得很好,但在测试时却引起了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72896441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档