tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/20-how-to-test-a-command-with-flexunit4Robotlegs: Discussion 2018-10-18T16:35:07Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/7158042009-12-12T17:05:58Z2009-12-12T17:05:58ZHow to test a Command with FlexUnit4?<div><p>I don't think you need to test that Robotlegs actually fires a
command. The framework itself has rather good unit tests.</p>
<p>If you are integration testing, I'd say the important bit is if
the Command execute method does the work it is supposed to do. This
would entail creating an instance of the Command, supplying its
dependencies (manually), calling execute, and then testing that the
work is performed properly on the dependencies.</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/7158042009-12-12T19:53:50Z2009-12-12T19:53:50ZHow to test a Command with FlexUnit4?<div><p>Another way, which is almost the same as Joel's suggestion
above, is to use an injector to instantiate your command - that way
an Error will be thrown if you forget to supply any of the
Command's dependencies. Something like this:</p>
<p>var injector:Injector = new Injector();<br>
injector.mapValue( SomeDependency, new SomeMockDependency() );<br>
var command:Command = injector.instantiate( Command );<br>
command.execute();</p>
<p>If you are testing a suite of Commands, you could shift some of
that into setup/teardown methods.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/7158042009-12-12T21:22:24Z2011-10-11T09:45:57ZHow to test a Command with FlexUnit4?<div><p>@Joel No, I'm not creating tests for the framework, I'm having a
hard enough time with my own apps :-)</p>
<p>I tried to instantiate my command directly, but in a certain
case, an event is thrown from within the command (which I forgot to
mention in my first post...). It would be nice to test the handling
of that event too.</p>
<p>@Shaun Would this work if I'd use the Injector? Btw, where can I
find the Injector class to import, or does my test class needs to
extend some base class?</p></div>Gert-Jan van der Weltag:robotlegs.tenderapp.com,2009-10-18:Comment/7158042009-12-12T21:32:50Z2009-12-12T21:32:50ZHow to test a Command with FlexUnit4?<div><p>If you want to test for an event create an async test method
and<br>
listen for the event.</p>
<p>Isolation and explicitness is the key. Test for one thing.</p>
<p>I personally don't think automated dependency injection has any
place<br>
in unit tests, but I'm a bit pedantic about the whole thing ;)</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/7158042009-12-12T21:45:43Z2011-10-11T09:45:57ZHow to test a Command with FlexUnit4?<div><p>Okay, so if I understand correctly I should be able to test my
command without any knowledge from the outside world (in
isolation). I guess this means that throwing events from a command
isn't best practise and I should refactor it?</p></div>Gert-Jan van der Weltag:robotlegs.tenderapp.com,2009-10-18:Comment/7158042009-12-12T22:00:30Z2009-12-12T22:00:30ZHow to test a Command with FlexUnit4?<div><p>Commands are perfectly suited for dispatching events. You can
simply<br>
test that the event is in fact dispatched (but not be concerned
with<br>
other classes in the broader app that would listen/respond to the
event)</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/7158042009-12-12T22:07:37Z2009-12-12T22:07:37ZHow to test a Command with FlexUnit4?<div><p>Regarding the injector route:</p>
<p>You can use org.robotlegs.adapters.SwiftSuspendersInjector or
SwiftSuspender's Injector directly.</p>
<p>One (major) annoyance with going this route is that you will
have to supply rules for all the injection points (dependencies)
declared on the base Command (org.robotlegs.mvcs.Command) if you
extended it, of which there are 5 (!), which seems like quite a big
price to pay for the single benefit of dependency checking.</p>
<p>Perhaps it's simpler and cleaner to just instantiate the Command
yourself and supply the required dependencies manually (just the
ones you actually need). At the end of the day, your test will
explode if it is missing a dependency in any case.</p>
<p>If your Command dispatches an event, be sure to supply it's
IEventDispatcher dependency:</p>
<p>var command:MyTestCommand = new MyTestCommand();<br>
command.eventDispatcher = new EventDispatcher();<br>
command.someOtherDependency = new SomeOtherDependency();<br>
command.execute();</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/7158042009-12-12T22:24:11Z2011-10-11T09:45:58ZHow to test a Command with FlexUnit4?<div><p>Thanks guys! This was really helpful.</p></div>Gert-Jan van der Wel