Property Injection in MVC CustomFilters though unity

Jul 16, 2014 at 1:28 PM
I am trying to create a similar scenario as below:
public class CustomAuthFilter: AuthorizeAttribute
public IClientService ClientSrvc { get; set; }

public override void OnAuthorization(AuthorizationContext filterContext)
        if (filterContext.HttpContext.User.Identity.IsAuthenticated)

I installed unity bootstrapper nuget and registered the type in unityconfig.cs as below:
unityContainer.RegisterType<IClientService, ClientService>();

When I run my MVC application , the IClientService resolves to ClientService Object .

The issue is , I created a seperate unittest Project to test my custom filter and when testing the unit test ,

the property dependency injection is not getting resolved.

I registered the types in the unit test project.

Can anyone please help me how to resolve the dependency from unit test project?
Jul 17, 2014 at 6:13 AM
If you register the type mappings and resolve using the container then dependencies should be injected.

More specifics would depend on what you are trying to test. I would just test the filter on its own and mock any web related context (e.g. HttpContext). If testing is the goal you could also just manually set the property (with, I assume, some sort of mock IClientService) without using a container.

Randy Levy
Enterprise Library support engineer
Support How-to
Jul 17, 2014 at 1:01 PM
Edited Jul 17, 2014 at 1:07 PM
I did the exact way.
I created a Unit Test Project and added references to my mvc app dlls and create a new test class and in my test class i created unity container and created the mocks and registered the types.

I wrote a testcontroller in the test project and add my custAuth filter to the controller and invoked the controller. but the DI is not happening and the IClientService ClientSrvc property is not getting set .

I have a doubt whether the mock objects are working fine or not so I checked whether the actual dependency Injection working without creating mock objects and registered the types like this
1.IUnityContainer container = new UnityContainer();
3.var test = container.Resolve<IClientService>();  // This statement worked fine and i got clientservice object .

4. var controller = new TestController();
6  Assert.IsTrue(new ActionInvokerExpecter<ViewResult>().InvokeAction(controller.ControllerContext, "TestAction"));
But when i invoke the controller in the above line 6, The code is hitting my "CustomAuthFilter" class but the property 'ClientSrvc ' is not getting resolved and it's just null.

Please help me.
Jul 17, 2014 at 9:27 PM
For unit tests, I would test the filter and the controller separately on their own. Typically filters are orthogonal to the functioning of the controller itself so it makes sense to keep the tests separate. Also, if a test fails you have a better idea who the culprit is. That said, I can see that you also might want to test how the controller functions with the filters applied.

Applying filters to the controllers is the responsibility of the ASP.NET MVC Framework so if you want to test how filters are applied to the controller you will need to wire that up yourself. Here's an (older) example using Moq: .

You could use Unity to wire up the dependencies and resolve and the dependencies will be injected provided the objects are resolved from the container.

Randy Levy
Enterprise Library support engineer
Support How-to
Jul 23, 2014 at 2:00 AM
Hi Randy,

Thank you so much . Your suggestion made me know what i was doing wrong and correct.
Once again thank you so much.