使用VS2010进行单元测试
我正在对一个方法进行单元测试,比如 ABC()
myclass target = new myclass();
target.ABC();
这个 ABC() 方法依次调用来自不同类的另一个方法 XYZ(),例如 anotherclass.XYZ()
和这个XYZ() 方法的输入参数取决于文本框中的值。
由于在测试运行时没有传递文本框中的值,因此我在运行测试时收到空引用异常(我的意思是测试因异常而失败)。
类似的情况(另一个):方法从查询字符串中获取 id 值,
HttpContext context
id = context.request.querystring["id"]
因为,在测试运行时,此 id 值为空,我得到一个空引用异常(我的意思是测试因异常而失败)。
据我了解,逻辑上这是正确的,因为这是测试运行而不是实际运行,但仍然想确认一次......
我做错了什么吗?
或者
我的测试运行正常吗?
请建议。感谢您的帮助。
谢谢。
I am unit testing a method say ABC()
like
myclass target = new myclass();
target.ABC();
This ABC() method in turn calls another method XYZ() from a different class like anotherclass.XYZ()
and this XYZ() method's input parameter depends on values from textbox (s).
Since, at test run there is no values from textbox passed I am getting a null referrence exception while running the test (I mean test fails with exception).
Similar situation (another): method fetches id value from querystring like
HttpContext context
id = context.request.querystring["id"]
Since, at test run this id values is null I get a null referrence exception(I mean test fails with exception).
To my understanding, logically this is correct since this is test run and not actual run but still want to confirm once ...
Am I doing anything wrong?
OR
My test is functioning correct?
Please suggest. Appreciate your help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你的代码是什么样的,但它可能需要稍微重构一下以允许单元测试。
例如,如果您有这样的代码:
您可能希望将其更改为这样的内容:
然后您可以使用模拟框架来模拟 IAnotherClass,如 IceN 所示。至于 HttpContext - 这变得有点复杂,但如果它仅由 AnotherClass 使用,您将不需要模拟它,因为模拟对象只会返回您喜欢的任何内容。显然,当您测试 AnothrClass 时,它就成为一个问题。如果您发现确实需要模拟它,那么有很多示例可以说明该怎么做 - 例如 这个。
I have no idea what your code looks like but it probably needs to be refactored a little bit to allow unit testing.
For instance, if you have code like this:
You would want to change it to something like this:
You can then use a mocking framework to mock up the IAnotherClass as IceN has show. As for the HttpContext - that becomes a little more complicated, but if it's only used by the AnotherClass you wont need to mock it since the mock object will just return whatever you like. Obviously when you come to test AnotehrClass it becomes an issue. If you find you do need to mock it then there are a number of examples of what to do - for instance this one.
您需要使用
anotherclass
的“非真实”实例,而是使用假XYZ
实现的模拟实例。我将用 Moq 框架做一个例子。
首先,您需要提取类
anotherclass
的接口才能创建 Mock。在我的示例中,我使用了一个接口IAnotherclass
。为什么我们要这样做?我们不干涉
anotherclass
类的逻辑。我们假设它是正确的。因此,我们从中进行抽象并仅测试myclass
的逻辑。让我们只验证 XYZ 方法已被调用。You need to use "not real" instance of
anotherclass
, but Mocked instance with fakeXYZ
implementation.I'll make an example with Moq framework.
First of all you need to extract an interface of class
anotherclass
in order to create Mock. In my example I used an interfaceIAnotherclass
.Why we are doing like that? We do not intresded in logic of class
anotherclass
. We assume it is correct. So we abstracting from it and testing only logic ofmyclass
. And leter we only veryfy that methodXYZ
is has been called.