测试Web.Api ActionFilter时模拟HttpActionContext.ActionArguments [英] Mocking HttpActionContext.ActionArguments when testing Web.Api ActionFilter

查看:1094
本文介绍了测试Web.Api ActionFilter时模拟HttpActionContext.ActionArguments的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个OnActionExecuting操作过滤器,我想对该功能进行单元测试.

I am writing an OnActionExecuting action filter and I want to unit test the functionality.

过滤器需要做的一件事是对传递到过滤器中的动作参数执行一些验证.

One of the things the filter needs to do is to performs some validation on the action arguments passed into the filter.

我从actionContext.ActionArguments字典中获取了参数,该字典对实现很有效,但是我很难对它进行单元测试.

I am getting the arguments from the actionContext.ActionArguments Dictionary which is working fine for the implementation but I am having a difficult time managing to unit test it.

在我的测试中,我无法设置actionContext.ActionArguments,因为它没有设置器,也不能模拟它,因为它不是虚拟的.

In my test I cannot set actionContext.ActionArguments as it has no setter an nor can I mock it as it is not virtual.

在这种情况下,我是否可以从单元测试中获得任何价值,这让我有点疑惑?

This leaved me in a bit of a quandary as to whether I can get any value from unit tests in this scenario?

推荐答案

根据AspNetWebStack源代码,actionContext.ActionArguments只是一个简单的Dictionary.因此,将键值对插入其中非常简单.我会做类似的事情

According to the AspNetWebStack source code, actionContext.ActionArguments is just a simple Dictionary. It is therefore pretty trivial to insert a key value pair into it. I would just do something like

actionContext.ActionArguments [key] = value;

actionContext.ActionArguments[key] = value;

在单元测试的安排部分.

in the arrange part of the unit test.

希望有帮助

这篇关于测试Web.Api ActionFilter时模拟HttpActionContext.ActionArguments的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆