tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/931-tdd-how-best-to-test-a-service-functionRobotlegs: Discussion 2012-08-27T10:38:00Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/163316672012-06-02T15:00:39Z2012-06-02T15:00:39Z[TDD] How best to test a Service function?<div><p>Hi there,</p>
<p>I hope to get a response regarding writing unit tests.</p>
<p>Thank you.</p></div>kimcitytag:robotlegs.tenderapp.com,2009-10-18:Comment/163316672012-06-04T17:23:27Z2012-06-04T17:23:27Z[TDD] How best to test a Service function?<div><p>Hi there,</p>
<p>anyone has a clue?</p></div>kimcitytag:robotlegs.tenderapp.com,2009-10-18:Comment/163316672012-06-06T11:07:07Z2012-06-06T11:07:07Z[TDD] How best to test a Service function?<div><p>The way I have typically tested this in the past is to mock file
FileReferenceList for example:</p>
<p><code>[Mock] public var
fileReferenceList:FileReferenceList;</code></p>
<p>I then stub getters on a mock FileReference for example:</p>
<p><code>private static const
JPG_FILE:String="test.jpg";</code></p>
<p><code>[Mock] public var file:FileReference;</code></p>
<p>Which are then stubbed like so:</p>
<pre>
<code> stub(file).getter("name").returns(JPG_FILE);
stub(file).getter("size").returns(1);
stub(fileReferenceList).getter("fileList").returns([file]);</code>
</pre>
<p>And here are some example tests:</p>
<pre>
<code> [Test]
public function should_call_browse_on_FileReferenceList():void
{
instance.browseFiles(fileReferenceList);
assertThat(instance.fileReferenceList, received().method('browse').anyArgs());
}
[Test]
public function should_dispatch_cancelBrowseFilesSignal_on_cancel():void
{
stub(fileReferenceList).method("browse").anyArgs().dispatches(new Event(Event.CANCEL, true, false));
instance.browseFiles(fileReferenceList);
assertThat(instance.cancelBrowseFilesSignal, received().method('dispatch').anyArgs());
}
[Test]
public function should_dispatch_displayErrorSignal_when_file_size_is_greater_than_max():void
{
stub(file).getter("name").returns(JPG_FILE);
stub(file).getter("size").returns(FileReferenceService.MAX_FILE_SIZE+1);
stub(fileReferenceList).getter("fileList").returns([file]);
instance.fileReferenceList=fileReferenceList;
instance.selectFiles(null);
assertThat(instance.displayErrorSignal, received().method('dispatch'));
}</code>
</pre>
<p>I hope that helps man,</p>
<p>Si</p></div>simon