tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/9362-iinjector-mockito-mock-builderRobotlegs: Discussion 2015-06-02T08:47:19Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/353791192014-11-25T07:40:22Z2014-11-25T07:40:22ZIInjector Mockito Mock Builder?<div><p>I was thinking something like this, but I get the Something like
this...<br>
<strong>ArgumentError: A proxy for [Class FQDN] has not been
prepared yet</strong> error. I'm not sure if it's possible to
prepare a class at run-time.</p>
<pre>
<code>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);
}
}
}</code>
</pre></div>hays.clarktag:robotlegs.tenderapp.com,2009-10-18:Comment/353791192014-11-25T08:06:32Z2014-11-25T08:06:32ZIInjector Mockito Mock Builder?<div><p>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.</p>
<p>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...</p>
<pre>
<code> [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;
}</code>
</pre>
<pre>
<code>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);
}
}
}</code>
</pre></div>hays.clark