Using asMock, how can I satisfy a concrete and interface requirement in SetupResult.forCall

178 Views Asked by At

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;
                    }
                }
            }
2

There are 2 best solutions below

0
On

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.

0
On

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.