我有一个工作单元测试,它设置一个TestServer,创建请求并验证响应。
当我从运行这些单元测试时,它们工作得很好。设置TestServer,发出请求并返回正确的响应。
当我从命令行(在我们的CI服务器上)运行相同的代码时,TestServer总是返回"Not“的状态代码。没有其他例外。在从命令行启动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");
}
}而且,正如我所说的,测试在中运行得很好。所有其他单元测试都按预期从命令行执行。
非常感谢!
发布于 2017-03-09 04:31:23
我找到了解决问题的办法。出现NotFound错误是因为我想测试的OAuth配置在作为单元测试运行时将AllowInsecureHttp属性设置为false。我确保AllowInsecureHttp不仅在DEBUG模式下是true,而且在单元测试运行时也是如此。
现在它工作得很好。
https://stackoverflow.com/questions/39999625
复制相似问题