【发布时间】:2019-09-26 17:36:21
【问题描述】:
我们有一个新的 XUnit 测试 .NET Core 2.x 项目,它正在使用 .NET Framework 4.6.1 项目测试解决方案。调用日志时...
Log.Info(string.Format("Services - Begin...\nURL: {0} {1}\nREQUEST: {2}",postRequest.RequestUri, postRequest.subURL, postRequest.Body.BodyString), this);
...我们遇到了一个 Sitecore 异常...
"The type initializer for 'Sitecore.DependencyInjection.DefaultSitecoreServicesConfigurator' threw an exception."
... 有一个内部异常,这似乎是问题的症结所在。
{"Could not load type 'System.Web.HttpContextBase' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.":"System.Web.HttpContextBase"}
查看它,似乎我得到的唯一答案是 System.Web.HttpContextBase 位于 .NET Core 和 .NET Framework 之间的不同系统库中,但我找不到太多信息或如何解决它。我不知道如何强制单元测试使用其中一个。
我目前无法将 .NET Framework 项目更改为 .NET Core(我们正在实施这些测试以便修复它。最终,一切都将成为 .NET Core,但现在我需要拥有这个 .NET Core testing project 测试解决方案中的所有 .NET Framework 库和应用程序。如果我删除那行日志记录代码(我们不想这样做),测试实际上可以工作。我需要在我的 xUnit 中添加或更改什么.NET Core 单元测试以通过这些使我们的代码失败的日志记录语句?
作为参考,这是我的示例测试和一些上下文支持代码:
Hashtable servicesDelegatePaths = new Hashtable();
servicesDelegatePaths.Add("CheckChild", "web/do/CheckChildBelongsToParent/CheckChildBelongsToParent");
public void CheckChild_Success()
{
var restStatus = new RestStatus
{
Status = new JsonResponseStatus
{
code = 200,
desc = "Record Matched",
key = ""
},
Data = "This is the reply data"
};
SetupRalAndHttpClient(JsonConvert.SerializeObject(restStatus));
var response = ral.CheckChild("123456978", "Jim", "Smith", "1234", new DateTime(2010, 1, 1));
Assert.NotNull(response.Result);
Assert.NotNull(response.Result.Status);
Assert.Equal(200, response.Result.Status.code);
Assert.Equal("Record Matched", response.Result.Status.desc);
}
private void SetupRalAndHttpClient(string httpResponseMessageContentString)
{
var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(httpResponseMessageContentString)
})
.Verifiable();
var httpClient = new HttpClient(handlerMock.Object);
ral = new RAL(domainURL, httpClient);
ral.DelegatePaths = servicesDelegatePaths;
}
【问题讨论】:
-
HttpContextBase 不是 .net 核心的一部分。不要尝试混合使用 .net 核心和 .net 框架的东西
-
正如我所提到的,我知道很多。我需要的是能够从我们的代码中安全地删除它或找到运行单元测试的 .net 核心等效项。
-
我们已决定将 xUnit 作为一个组织,xUnit 仅在 .NET Core 中可用。在我们插入单元测试之前,我也无法更改项目。
-
xunit 在 .net 框架中工作。
-
从 asp.net 中移出 asp.net 核心并不是简单地翻转开关。这是一个需要重写的新范式。
标签: c# asp.net .net asp.net-core xunit