There is an ActionFilter
on my controller-class. The OnActionExecuting
method gets called, when an action of the controller is called in a web application.
我的控制器类上有一个ActionFilter。在Web应用程序中调用控制器的操作时,将调用OnActionExecuting方法。
Now I call the Action
in a UnitTest:
现在我在UnitTest中调用Action:
NiceController niceController = new NiceController();
ActionResult result = niceController.WhateverAction();
Is there a way to have the ActionFilter called?
有没有办法让ActionFilter调用?
2 个解决方案
#1
9
In order to have the ActionFilter called automatically, you are going to need to run the controller action invoker. This is possible, but it means that the MVC framework will try and execute the result. This means that you would have to use mocks to stub out the execution of the result. Again, that is possible, but it means that your unit test is becoming more mocks than actual code. It may be more correct to just test the filter directly. After all, the fact that OnActionExecuting is called is a feature of the framework, and you don't need to unit test the framework itself.
为了自动调用ActionFilter,您将需要运行控制器动作调用程序。这是可能的,但这意味着MVC框架将尝试并执行结果。这意味着您必须使用模拟来删除结果的执行。同样,这是可能的,但这意味着您的单元测试变得比实际代码更多。直接测试滤波器可能更正确。毕竟,OnActionExecuting被调用的事实是框架的一个特性,您不需要对框架本身进行单元测试。
But I think that what you're really saying is that you want to test WhateverAction, and that action cannot work unless the ActionFilter has executed.
但我认为你真正要说的是你要测试WhateverAction,除非ActionFilter已执行,否则该操作无法运行。
First, I would ask questions about this design. Is this correct? It might be. It is reasonable, for example, that an action with the Authorize attribute could presume that when it executes there is a logged in user. Of course, the action should test this, but the presumption is safe. On the other hand, actions should probably not require filters to do action-specific initialization. So you should ask the question, but the answer they well be that the design is correct.
首先,我会问这个设计的问题。它是否正确?有可能。例如,具有Authorize属性的动作可以假设在执行时有一个登录用户是合理的。当然,行动应该对此进行测试,但这种假设是安全的。另一方面,操作可能不需要过滤器来执行特定于操作的初始化。所以你应该问这个问题,但他们的答案是设计是正确的。
In this case, the best decision for a unit test might be to manually execute the filter in the unit test, and to write a separate unit test which proves that the action is decorated with the correct attribute.
在这种情况下,单元测试的最佳决策可能是在单元测试中手动执行过滤器,并编写单独的单元测试,以证明操作使用正确的属性进行修饰。
#2
0
to write a separate unit test which proves that the action is decorated with the correct attribute
编写一个单独的单元测试,证明该操作是用正确的属性修饰的
Here is how you can write such a unit test
以下是如何编写这样的单元测试
Type t = typeof(MyController);
Assert.IsTrue(t.GetCustomAttributes(typeof(MyCustomAttribute)).Length > 0);
#1
9
In order to have the ActionFilter called automatically, you are going to need to run the controller action invoker. This is possible, but it means that the MVC framework will try and execute the result. This means that you would have to use mocks to stub out the execution of the result. Again, that is possible, but it means that your unit test is becoming more mocks than actual code. It may be more correct to just test the filter directly. After all, the fact that OnActionExecuting is called is a feature of the framework, and you don't need to unit test the framework itself.
为了自动调用ActionFilter,您将需要运行控制器动作调用程序。这是可能的,但这意味着MVC框架将尝试并执行结果。这意味着您必须使用模拟来删除结果的执行。同样,这是可能的,但这意味着您的单元测试变得比实际代码更多。直接测试滤波器可能更正确。毕竟,OnActionExecuting被调用的事实是框架的一个特性,您不需要对框架本身进行单元测试。
But I think that what you're really saying is that you want to test WhateverAction, and that action cannot work unless the ActionFilter has executed.
但我认为你真正要说的是你要测试WhateverAction,除非ActionFilter已执行,否则该操作无法运行。
First, I would ask questions about this design. Is this correct? It might be. It is reasonable, for example, that an action with the Authorize attribute could presume that when it executes there is a logged in user. Of course, the action should test this, but the presumption is safe. On the other hand, actions should probably not require filters to do action-specific initialization. So you should ask the question, but the answer they well be that the design is correct.
首先,我会问这个设计的问题。它是否正确?有可能。例如,具有Authorize属性的动作可以假设在执行时有一个登录用户是合理的。当然,行动应该对此进行测试,但这种假设是安全的。另一方面,操作可能不需要过滤器来执行特定于操作的初始化。所以你应该问这个问题,但他们的答案是设计是正确的。
In this case, the best decision for a unit test might be to manually execute the filter in the unit test, and to write a separate unit test which proves that the action is decorated with the correct attribute.
在这种情况下,单元测试的最佳决策可能是在单元测试中手动执行过滤器,并编写单独的单元测试,以证明操作使用正确的属性进行修饰。
#2
0
to write a separate unit test which proves that the action is decorated with the correct attribute
编写一个单独的单元测试,证明该操作是用正确的属性修饰的
Here is how you can write such a unit test
以下是如何编写这样的单元测试
Type t = typeof(MyController);
Assert.IsTrue(t.GetCustomAttributes(typeof(MyCustomAttribute)).Length > 0);