IInjector Mockito Mock Builder?

hays.clark's Avatar

hays.clark

25 Nov, 2014 07:02 AM

Anyone have a Mockito-Flex IInjector Answer or AutoMocker class that will take the place of SwiftSuspenders when testing?

I have a StateMachine factory under test, and it' used IInjector.injectInto when constructing a StateMachine. The problem is that the builder sets the initial state which actually run's the initial states's onEnter() method. This method is throwing a 1009 error because it's missing a value that is expected.

I was someone has a cleaver IInjector substitute class that Inject's mocks. :D

  1. 1 Posted by hays.clark on 25 Nov, 2014 07:40 AM

    hays.clark's Avatar

    I was thinking something like this, but I get the Something like this...
    ArgumentError: A proxy for [Class FQDN] has not been prepared yet error. I'm not sure if it's possible to prepare a class at run-time.

    package testUtils
    {
        import flash.utils.getDefinitionByName;
        import flash.utils.getQualifiedClassName;
        
        import org.mockito.api.Answer;
        import org.mockito.api.StubbingContext;
        import org.mockito.api.StubbingContextAware;
        import org.mockito.integrations.mock;
        import org.mockito.integrations.flexunit4.MockitoRule;
        import org.robotlegs.adapters.SwiftSuspendersInjector;
        import org.robotlegs.core.IInjector;
        
        /**
         * Used for mocking when the parameters being passed into the call need to be inspected
         * in detail more than the normal matchers for example object property count
         */
        public class SwiftSuspendersInjectorAnswer implements Answer, StubbingContextAware
        {   
            //----------------------------------
            //  vars
            //----------------------------------
            public var callArgs:Array; //Vector.<Array>
            
            /**
             * Delegates the answer to a given callback. Whenever the stubbed
             * function is called, the call will be delegated to this callback, and the
             * original arguments will be passed.
             */
            private var context:StubbingContext;
            private var _realzInjector:IInjector;
            private var _rule:MockitoRule;
            
            //--------------------------------------------------------------------------
            //
            //  PUBLIC METHODS
            //
            //--------------------------------------------------------------------------
            public function SwiftSuspendersInjectorAnswer(rule:MockitoRule) {
                _realzInjector = new SwiftSuspendersInjector();
                _rule = rule;
                callArgs = []; //callArgs = new Vector.<Array>();
            }
            
            public function useContext(stubbingContext:StubbingContext):void {
                context = stubbingContext;
            }
            
            public function give():* {
                callArgs.push(context.args);
                var fqdn:String = getQualifiedClassName(context.args[0]);
                var clazz:Class = getDefinitionByName(fqdn) as Class;
                _realzInjector.mapValue(clazz, mock(clazz));
                return _realzInjector.injectInto.apply(null, context.args);
            }
        }
    }
    
  2. 2 Posted by hays.clark on 25 Nov, 2014 08:06 AM

    hays.clark's Avatar

    Here is how I solved the problem for now... let me know is someone has something better that can figure out the mocks on the fly.

    I am just preparing the classes that I expect to be injected. I create and Array of the classes and hand them to the Answer. The Answer is using a real SwiftSuspendersInjector Injector, but its setup to inject mocks. The injector itself is only part of the setup process, and all the classes are using Mocks. I think it's a pretty sound setup...

         [Before]
            public function setUp():void {
                _instance = new ClassUnderTest();
                _instance.injector = createInjector();
            }
    
            // ....
    
            private function createInjector():IInjector {
                var preparedClasses:Array = [
                    IInjector,
                    IMediator,
                    IMediatorMap,
                    IEventMap,
                    ICommandMap,
                    IEventDispatcher
                ];
                var injector:IInjector = mockInjector;
                given(mockInjector.injectInto(any()))
                    .will(new SwiftSuspendersInjectorAnswer(preparedClasses));
                return injector;
            }
    
    package testUtils
    {
        import org.mockito.api.Answer;
        import org.mockito.api.StubbingContext;
        import org.mockito.api.StubbingContextAware;
        import org.mockito.integrations.mock;
        import org.robotlegs.adapters.SwiftSuspendersInjector;
        import org.robotlegs.core.IInjector;
        
        /**
         * Used for mocking when the parameters being passed into the call need to be inspected
         * in detail more than the normal matchers for example object property count
         */
        public class SwiftSuspendersInjectorAnswer implements Answer, StubbingContextAware
        {   
            //----------------------------------
            //  vars
            //----------------------------------
            public var callArgs:Array; //Vector.<Array>
            
            /**
             * Delegates the answer to a given callback. Whenever the stubbed
             * function is called, the call will be delegated to this callback, and the
             * original arguments will be passed.
             */
            private var context:StubbingContext;
            private var _injector:IInjector;
            
            //--------------------------------------------------------------------------
            //
            //  PUBLIC METHODS
            //
            //--------------------------------------------------------------------------
            public function SwiftSuspendersInjectorAnswer(preparedClasses:Array) {
                _injector = new SwiftSuspendersInjector();
                for each (var clazz:Class in preparedClasses) {
                    _injector.mapValue(clazz, mock(clazz));
                }
                callArgs = []; //callArgs = new Vector.<Array>();
            }
            
            public function useContext(stubbingContext:StubbingContext):void {
                context = stubbingContext;
            }
            
            public function give():* {
                return _injector.injectInto.apply(null, context.args);
            }
        }
    }
    
  3. Ondina D.F. closed this discussion on 02 Jun, 2015 08:47 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac