IHeadersDictionary 在模拟后返回 null
IHeadersDictionary returning null after mocking
我正在尝试模拟 IHeadersDictionary,每当我尝试访问它时,我都会 returns Null。
public interface IRequestScopeContext
{
IHeaderDictionary Headers { get; set; }
ISessionInfo SessionInfo { get; set; }
HttpContext HttpContextInfo { get; set; }
}
[SetUp]
public void Setup()
{
var headers = new Dictionary<string, string>
{
{ "Key", "Value"}
} as IHeaderDictionary;
var sessionInfo = new SessionInfo
{
AccountId = "AccountId",
UserId = "UserId",
};
requestScopeContext = new Mock<IRequestScopeContext>();
requestScopeContext.Setup(x => x.Headers).Returns(headers);
requestScopeContext.Setup(x => x.SessionInfo).Returns(sessionInfo);
serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(sp => sp.GetService(It.Is<Type>((Type t) => t.Name.Equals("IRequestScopeContext")))).Returns(requestScopeContext.Object);
httpContextAccessor = new Mock<IHttpContextAccessor>();
httpContextAccessor.Setup(x => x.HttpContext.RequestServices).Returns(serviceProvider.Object);
}
我也试过使用
requestScopeContext.Setup(x => x.Headers.Add( "Key", "Value"));
但每当我访问 requestScopeContext.Headers
时,它 returns 我都是空的。
我应该如何模拟这本词典?
我的测试方法是这样的
[Test]
public void SessionHelper_InvokeConstructor_Should_ReturnValidObject()
{
var sessionHelper = new SessionHelper(httpContextAccessor.Object);
Assert.IsNotNull(sessionHelper);
}
这是我正在测试的一段代码。
public SessionHelper(IHttpContextAccessor httpContextAccessor)
{
IRequestScopeContext requestScopeContext = (IRequestScopeContext)httpContextAccessor.HttpContext.RequestServices.GetService(typeof(IRequestScopeContext));
currentAccountId = requestScopeContext.SessionInfo?.AccountId;
currentUserId = requestScopeContext.SessionInfo?.UserId;
requestId = requestScopeContext.Headers?["key"];
}
如@Iurii Maksimov 所述,您的转换不正确 - Dictionary<string, string>
和 IHeaderDictionary
之间没有直接转换,请改用:
var headers = new HeaderDictionary(new Dictionary<String, StringValues>
{
{ "Key", "Value"}
}) as IHeaderDictionary;
你确定你的选角成功了吗?
var headers = new Dictionary<string, string>
{
{ "Key", "Value"}
} as IHeaderDictionary;
这里我快速测试了最小起订量。配置正确。
我正在尝试模拟 IHeadersDictionary,每当我尝试访问它时,我都会 returns Null。
public interface IRequestScopeContext
{
IHeaderDictionary Headers { get; set; }
ISessionInfo SessionInfo { get; set; }
HttpContext HttpContextInfo { get; set; }
}
[SetUp]
public void Setup()
{
var headers = new Dictionary<string, string>
{
{ "Key", "Value"}
} as IHeaderDictionary;
var sessionInfo = new SessionInfo
{
AccountId = "AccountId",
UserId = "UserId",
};
requestScopeContext = new Mock<IRequestScopeContext>();
requestScopeContext.Setup(x => x.Headers).Returns(headers);
requestScopeContext.Setup(x => x.SessionInfo).Returns(sessionInfo);
serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(sp => sp.GetService(It.Is<Type>((Type t) => t.Name.Equals("IRequestScopeContext")))).Returns(requestScopeContext.Object);
httpContextAccessor = new Mock<IHttpContextAccessor>();
httpContextAccessor.Setup(x => x.HttpContext.RequestServices).Returns(serviceProvider.Object);
}
我也试过使用
requestScopeContext.Setup(x => x.Headers.Add( "Key", "Value"));
但每当我访问 requestScopeContext.Headers
时,它 returns 我都是空的。
我应该如何模拟这本词典?
我的测试方法是这样的
[Test]
public void SessionHelper_InvokeConstructor_Should_ReturnValidObject()
{
var sessionHelper = new SessionHelper(httpContextAccessor.Object);
Assert.IsNotNull(sessionHelper);
}
这是我正在测试的一段代码。
public SessionHelper(IHttpContextAccessor httpContextAccessor)
{
IRequestScopeContext requestScopeContext = (IRequestScopeContext)httpContextAccessor.HttpContext.RequestServices.GetService(typeof(IRequestScopeContext));
currentAccountId = requestScopeContext.SessionInfo?.AccountId;
currentUserId = requestScopeContext.SessionInfo?.UserId;
requestId = requestScopeContext.Headers?["key"];
}
如@Iurii Maksimov 所述,您的转换不正确 - Dictionary<string, string>
和 IHeaderDictionary
之间没有直接转换,请改用:
var headers = new HeaderDictionary(new Dictionary<String, StringValues>
{
{ "Key", "Value"}
}) as IHeaderDictionary;
你确定你的选角成功了吗?
var headers = new Dictionary<string, string>
{
{ "Key", "Value"}
} as IHeaderDictionary;
这里我快速测试了最小起订量。配置正确。