【问题标题】:Start OWIN TestServer from mstest.exe从 mstest.exe 启动 OWIN TestServer
【发布时间】:2016-10-12 13:17:29
【问题描述】:

我有一个工作单元测试,它设置了一个 TestServer、创建请求并验证响应。

当我从 Visual Studio 测试资源管理器运行这些单元测试时,它们运行良好。 TestServer 已设置,发出请求并返回正确的响应。

当我从命令行(在我们的 CI 服务器上)运行相同的代码时,TestServer 总是返回“未找到”的状态代码。没有其他例外。从命令行启动 OWIN 时是否需要添加任何特定命令?

MSTest.exe 命令如下所示:

MSTest.exe /testcontainer:myTests.dll /resultsfile:testresults.trx

我的测试方法如下所示:

[TestMethod]
public async Task GetToken()
{
   using (var server = TestServer.Create<TestStartupConfiguration>())
   {
      var response = await server.CreateRequest("/TokenUrl").And(x => x.Content = new FormUrlEncodedContent(new[]
       {
           new KeyValuePair<string, string>("username", user.UserName),
           new KeyValuePair<string, string>("password", user.Password),
           new KeyValuePair<string, string>("grant_type", "password")
        })).PostAsync();

       response.IsSuccessStatusCode.Should().BeTrue("this is the expected return value of an Access Token request");
       var responseString = await response.Content.ReadAsStringAsync();
       responseString.Should().NotBeNullOrWhiteSpace("the response of an Access Token request should not be empty");
    }
}

而且,正如我所说,测试在 Visual Studio 中运行良好。并且所有其他单元测试都按预期从命令行执行。

非常感谢!

【问题讨论】:

  • 您的构建设置是否用于恢复 NuGet 包?

标签: c# unit-testing owin mstest


【解决方案1】:

我找到了解决问题的方法。出现NotFound 错误是因为我想测试的 OAuth 配置在作为单元测试运行时将AllowInsecureHttp 属性设置为false。我确保AllowInsecureHttp 不仅在DEBUG 模式下而且在单元测试运行时也是true

现在可以完美运行了。

【讨论】:

    猜你喜欢
    • 2014-12-18
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 2014-04-19
    相关资源
    最近更新 更多