使用asMock,如何满足SetupResult.forCall中的具体和接口要求
ValidationManager 有一个公共字典,用于存储实现 IValidatable 接口的 UI 组件。
我正在测试一个需要 ValidationManager 实例的命令类,并且我希望它无法通过验证。因此,我重写了 ValidationManager 的“validateItem()”方法,如下所示:
var validationManagerRepos:ValidationManager = ValidationManager(mockRepository.createStub(ValidationManager));
var validationItem:IValidatable = IValidatable(mockRepository.createStub(IValidatable));
var validatableItems:Dictionary = new Dictionary();
validatableItems[validationItem] = false;
SetupResult.forCall(validationManagerRepos.validateItem(validationItem)).returnValue(false);
我的问题出在命令的执行方法中。它检查validationItem是否既是DisplayObject (isVisble)又是IValidatable。有什么巧妙的方法来存根类型对象和接口吗?或者我只需要创建一个已经满足两者的现有对象的实例?
for (var iVal:Object in validationManager.validatableItems)
{
if (isVisible(DisplayObject(iVal)))
{
passed = validationManager.validateItem(IValidatable(iVal));
eventDispatcher.dispatchEvent(new ValidationEvent(ValidationEvent.VALIDATE_COMPLETED, IValidatable(iVal), passed));
if (!passed)
{
allPassed = false;
}
}
}
The ValidationManager has a public Dictionary for storing UI components that implement the IValidatable interface.
I am testing a command class that needs an instance of ValidationManager and I want it to fail the validations. So I override the ValidationManager's "validateItem()" method like so:
var validationManagerRepos:ValidationManager = ValidationManager(mockRepository.createStub(ValidationManager));
var validationItem:IValidatable = IValidatable(mockRepository.createStub(IValidatable));
var validatableItems:Dictionary = new Dictionary();
validatableItems[validationItem] = false;
SetupResult.forCall(validationManagerRepos.validateItem(validationItem)).returnValue(false);
My problem is in the execute method of the command. It checks to see if the validationItem is both a DisplayObject (isVisble) and IValidatable. Any slick way to stub a typed object AND an interface? Or do I just need to create an instance of some existing object that already satisfies both?
for (var iVal:Object in validationManager.validatableItems)
{
if (isVisible(DisplayObject(iVal)))
{
passed = validationManager.validateItem(IValidatable(iVal));
eventDispatcher.dispatchEvent(new ValidationEvent(ValidationEvent.VALIDATE_COMPLETED, IValidatable(iVal), passed));
if (!passed)
{
allPassed = false;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相当确定你不能在 asMock 中同时完成这两件事。由于缺乏多态性,这是 Flash Player 的限制。
我相信您需要做的是创建一个测试对象,该对象既执行这两项操作(扩展 DisplayObject 并实现 IValidatable),又创建一个模拟对象。
I'm fairly sure you can't do both within asMock. It's a limitation of the Flash Player because of lack of polymorphism.
I believe what you'll have to do is create a testing object that does both (extend DisplayObject and implement IValidatable) and create a mock object of that.
“multimock”的概念当然是可能的,但 floxy(asmock 用于生成动态代理的框架)不支持它。我之前考虑过添加对它的支持,但是很难通过各种
Mock
元数据公开,并且还有其他问题需要担心(例如方法名称冲突)。我同意 J_A_X 创建自定义类然后嘲笑它的建议。
The concept of a "multimock" is certainly possible, but floxy (the framework that asmock uses to generate dynamic proxies) doesn't support it. I previously considered adding support for it, but it would be difficult to expose via the various
Mock
metadata and there's be other issues to worry about (like method name clashes).I agree with J_A_X's recommendation of creating a custom class and then mocking that.