minimum requirements to mock HttpContext.GetRequestedApiVersion() #981
-
I'm wanting to test some functionality and setting up my test incorrectly so I'm asking for a little guidance. I have a little helper to create an public static HttpContext CreateVersionedHttpContext(int version) {
var defaultHttpContext = new DefaultHttpContext();
defaultHttpContext.Features.Set(new ApiVersioningFeature(defaultHttpContext) {
RequestedApiVersion = new ApiVersion(version, 0)
});
return defaultHttpContext;
} In my test I return that value from an injectable [Fact]
public void Should_return_v2_for_api_v2() {
var httpContext = HttpContextHelpers.CreateVersionedHttpContext(2);
var httpContextAccessor = new Mock<IHttpContextAccessor>();
httpContextAccessor.SetupGet(x => x.HttpContext).Returns(httpContext);
// ... In the class I'm testing I inject the var version = _httpContext.GetRequestedApiVersion(); If I look at the features of the These tests are helpful but they seem to setup a lot of things and are using the query string only. Are there tests for the other ways to set the version? This seems to work. var version = new ApiVersion(2, 0);
var feature = new Mock<IApiVersioningFeature>();
var featureCollection = new Mock<IFeatureCollection>();
var httpContext = new Mock<HttpContext>();
feature.SetupProperty(f => f.RequestedApiVersion, version);
featureCollection.Setup(fc => fc.Get<IApiVersioningFeature>()).Returns(feature.Object);
httpContext.SetupGet(hc => hc.Features).Returns(featureCollection.Object);
var httpContextAccessor = new Mock<IHttpContextAccessor>();
httpContextAccessor.SetupGet(x => x.HttpContext).Returns(httpContext.Object); Is there a suggested way for when using the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm glad you found the tests. That is the documentation. 😉 Setting up and parsing from the You've missed just one very small, but important piece. Wait for it.... Warning: You are going to kick yourself... 2 ... 3: public static HttpContext CreateVersionedHttpContext(int version) {
var context = new DefaultHttpContext();
IApiVersionVersioningFeature feature = new ApiVersioningFeature(context)
{
RequestedApiVersion = new ApiVersion(version, 0),
};
context.Features.Set(feature);
return context;
} Hopefully, you've spotted the issue, but if not, the problem is that you inlined
public static HttpContext CreateVersionedHttpContext(int version) {
var context = new DefaultHttpContext();
context.ApiVersioningFeature().RequestedApiVersion = new(version, 0);
return context;
} I hope that helps. |
Beta Was this translation helpful? Give feedback.
I'm glad you found the tests. That is the documentation. 😉
Setting up and parsing from the
HttpContext
is irrelevant and unnecessary. The specific tests you mention exercise that path for coverage, but it is unnecessary. I wouldn't recommend you re-test that behavior. If all you want to do is test the behavior for a specificApiVersion
, you can explicitly set it in the feature, which it seems you've figured out.You've missed just one very small, but important piece. Wait for it.... Warning: You are going to kick yourself... 2 ... 3: