在被测对象中模拟事件的策略 [英] Strategy to simulate events in object under test

查看:46
本文介绍了在被测对象中模拟事件的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于单元测试来说是全新的,我的意思是真的很新.我已经阅读了很多书,并且正在慢慢地走着,尝试遵循自己的最佳实践.我在Visual Studio 2010中使用MS-Test.

我遇到了一个我不太确定如何进行的要求.我正在研究负责与外部硬件进行交互的组件.该项目上还有更多开发人员,他们无法访问硬件,因此我实现了该组件的虚拟"或模拟实现,并将尽可能多的共享逻辑上移到了基类中. >

现在,在允许他们编译和运行代码的范围内,它可以很好地工作,但是对于模拟单元测试所需的事件和内部状态更改而言,它并不是非常有用(不要忘记我是测试的新手)

例如,我要测试的组件上有几个事件,但是我需要调用它们来测试它们.通常,要引发事件,我可以按下硬件上的按钮或分流两个终端,但在模拟对象中(显然),我不能这样做.

我有两个关注点/要求:

  1. 我需要为单元测试提供状态更改并引发事件
  2. 我需要提供状态更改并引发事件以供我的团队测试组件的依赖关系(例如,当发生某些硬件事件时,将启用WPF视图上的按钮)

对于后者,我想到了一些复杂的控制面板对话框,这些对话框使我可以触发事件并通常模拟硬件操作和用户交互.这很复杂,因为它需要没有消息泵的组件来提供带有控件的窗口.臭或者另一种方法是实现模拟组件以获取"StateInfo"对象,我可以使用它来更改对象的内部.

这不是一个新问题,我敢肯定你们中的许多人都必须做与此类似的事情,而我只是想知道您用来实现此目标的模式或策略.我知道我可以使用n访问器访问私有字段,但这并不能真正提供交互式(在运行时模拟的情况下)更改.

解决方案

如果库中有用于与外部硬件进行交互的接口,则可以为其创建一个模拟对象并从该单元中引发事件测试.

如果没有,则需要将硬件调用包装在包装器类中,以便对其进行模拟并提供测试中所需的行为.

有关如何从模拟对象引发事件的示例,请参见模拟比较-引发事件

希望对您有帮助!

BRAND NEW to unit testing, I mean really new. I've read quite a bit and am moving slowly, trying to follow best practices as I go. I'm using MS-Test in Visual Studio 2010.

I have come up against a requirement that I'm not quite sure how to proceed on. I'm working on a component that's responsible for interacting with external hardware. There are a few more developers on this project and they don't have access to the hardware so I've implemented a "dummy" or simulated implementation of the component and moved as much shared logic up into a base class as possible.

Now this works fine as far as allowing them to compile and run the code, but it's not terrible useful for simulating the events and internal state changes needed for my unit tests (don't forget I'm new to testing)

For example, there are a couple events on the component that I want to test, however I need them to be invoked in order to test them. Normally to raise the event I would push a button on the hardware or shunt two terminals, but in the simulated object (obviously) I can't do that.

There are two concerns/requirements that I have:

  1. I need to provide state changes and raise events for my unit tests
  2. I need to provide state changes and raise events for my team to test dependencies on the component (e.g. a button on a WPF view becomes enabled when a certain hardware event occurs)

For the latter I thought about some complicated control panel dialog that would let me trigger events and generally simulate hardware operation and user interaction. This is complicated as it requires a component with no message pump to provide a window with controls. Stinky. Or another approach could be to implement the simulated component to take a "StateInfo" object that I could use to change the internals of the object.

This can't be a new problem, I'm sure many of you have had to do something similar to this and I'm just wondering what patterns or strategies you've used to accomplish this. I know I can access private fields with a n accessor, but this doesn't really provide an interactive (in the case of runtime simulation) changes.

解决方案

If there is an interface on the library you use to interact with the external hardware you can just create a mock object for it and raise events from that in your unit tests.

If there isn't, then you'll need to wrap the hardware calls in a wrapper class so you mock it and provide the behaviours you want in your test.

For examples of how to raise events from mock objects have a look at Mocking Comparison - Raising Events

I hope that helps!

这篇关于在被测对象中模拟事件的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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