What are you trying to do a unit test or an integration test? if you are trying to do a unit test, then nothing you doing is deemed to be a unit test.
if (context.Request.GetDependencyScope().GetService(typeof(new ExternalService))
is IExternalService externalService)
the above is how you could resolve the dependency. The IoC will not work, as the IoC has to be used in what is going to use the IoC, the unit of code, and you test that unit of code that is actually going to use the object instanced in the IoC container.
If you find the post has answered your issue, then please mark post as 'answered'.
I have authentication filter and it works because I have unity container in my host and context.Request.GetDependencyScope().GetService(typeof(IExternalService)) works. Example https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-filters
public class MyCustomAuthenticationAttribute : Attribute, IAuthenticationFilter
{
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
HttpRequestMessage request = context.Request;
AuthenticationHeaderValue authorization = request.Headers.Authorization;
//If there are no credentials, do nothing.
if (authorization == null)
{
return;
}
..................
if (context.Request.GetDependencyScope().GetService(typeof(IExternalService)) is IExternalService externalService)
externalService.DoJob();
..................
}
}
Then I want to do Unit test as I wrote. It's expect result, because test doesn't resolve IExternalService. I want to do like this
public class ExternalServiceStub: IExternalService
{
public void DoJob()
{
do something;
}
}
context.Request.GetDependencyScope().GetService(typeof(IExternalService)) is IExternalService externalService is example from "ASP.NET Web API 2 Recipes" book 277 page
context.Request.GetDependencyScope().GetService(typeof(IExternalService))is IExternalService externalService is example from "ASP.NET Web API 2 Recipes" book 277 page
IExternalService should be mocked out using a mocking framework, which means that you would have to know the class/object that implements IExternalService and what it is doing internally.
If you find the post has answered your issue, then please mark post as 'answered'.
I added HttpConfiguration to my HttpActionContext and now context.Request.GetDependencyScope() doesn't throw System.NullReferenceException. Of cource context.Request.GetDependencyScope().GetService(typeof(IExternalService)) is null, but now It's ok for my
tests.
private System.Web.Http.Controllers.HttpActionContext CreateExecutingContext()
{
var config = new HttpConfiguration();
var httpActionContext = new System.Web.Http.Controllers.HttpActionContext
{
ControllerContext = new HttpControllerContext
{
Request = new HttpRequestMessage()
{
RequestUri = new Uri("http://TestApi/api/v1/Test"),
},
Configuration = config
}
};
httpActionContext.ControllerContext.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
return httpActionContext;
}
If I want to resolve dependency, I will can add DependencyResolver to my config or Mocking framework
Member
1 Points
23 Posts
Testing filters with IDependencyScope in Web API
Nov 13, 2017 04:31 PM|olegmkr|LINK
I have WebApi simple NUnit Test
and in TestAuthenticationAttribute I have
How to set/resolve IExternalService dependency in test? Do I need e.g. UnityContainer or I can do it without container?
Contributor
2821 Points
2572 Posts
Re: Testing filters with IDependencyScope in Web API
Nov 13, 2017 06:05 PM|DA924|LINK
http://www.artima.com/weblogs/viewpost.jsp?thread=126923
What are you trying to do a unit test or an integration test? if you are trying to do a unit test, then nothing you doing is deemed to be a unit test.
if (context.Request.GetDependencyScope().GetService(typeof(new ExternalService)) is IExternalService externalService)
the above is how you could resolve the dependency. The IoC will not work, as the IoC has to be used in what is going to use the IoC, the unit of code, and you test that unit of code that is actually going to use the object instanced in the IoC container.
Member
1 Points
23 Posts
Re: Testing filters with IDependencyScope in Web API
Nov 13, 2017 07:43 PM|olegmkr|LINK
I try to do unit test.
I have authentication filter and it works because I have unity container in my host and context.Request.GetDependencyScope().GetService(typeof(IExternalService)) works. Example https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-filters
Then I want to do Unit test as I wrote. It's expect result, because test doesn't resolve IExternalService. I want to do like this
context.Request.GetDependencyScope().GetService(typeof(IExternalService)) is IExternalService externalService is example from "ASP.NET Web API 2 Recipes" book 277 page
Contributor
2821 Points
2572 Posts
Re: Testing filters with IDependencyScope in Web API
Nov 13, 2017 10:24 PM|DA924|LINK
context.Request.GetDependencyScope().GetService(typeof(IExternalService)) is IExternalService externalService is example from "ASP.NET Web API 2 Recipes" book 277 page
IExternalService should be mocked out using a mocking framework, which means that you would have to know the class/object that implements IExternalService and what it is doing internally.
Member
1 Points
23 Posts
Re: Testing filters with IDependencyScope in Web API
Nov 14, 2017 06:37 AM|olegmkr|LINK
I added HttpConfiguration to my HttpActionContext and now context.Request.GetDependencyScope() doesn't throw System.NullReferenceException. Of cource context.Request.GetDependencyScope().GetService(typeof(IExternalService)) is null, but now It's ok for my tests.
If I want to resolve dependency, I will can add DependencyResolver to my config or Mocking framework