Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Test Controller with PerWebRequest LifeStyle dependency

I am trying to write a unit test for a controller that has a dependency on a type whose lifestyle is "PerWebRequest".

Castle throws the following exception:

System.InvalidOperationException: HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net.

Can I mock the HttpContext.Current property somehow to get around this?

I have tried to use MVCContrib's TestControllerBuilder to initialize this controller but it has no effect.

  SymptomTopicController controller = new SymptomTopicController();
        controller.WorkOrderFulfillment = workOrderFulfillment;

        TestControllerBuilder controllerBuilder = new TestControllerBuilder();
        controllerBuilder.InitializeController(controller);
like image 280
Nick Avatar asked Dec 14 '25 10:12

Nick


2 Answers

You're doing it wrong

Abstract the dependency and don't use container in your tests.

like image 112
Krzysztof Kozmic Avatar answered Dec 17 '25 00:12

Krzysztof Kozmic


Have you seen this question: Mock HttpContext.Current in Test Init Method ?

like image 32
Victor Haydin Avatar answered Dec 17 '25 02:12

Victor Haydin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!