[TDD] How best to test a Service function?

kimcity's Avatar

kimcity

31 May, 2012 08:21 AM

I have a Service called DropIn that has a function called UploadFile.

There is a Command called DropInUploadFileCommand which has the following execute function

    override public function execute():void {           
        service.uploadFile(event.file);
    }

I am now trying to write a unit test for the UploadFile function.

I followed the example by joelhooks for ImageGallery, but I realized that unlike his functions for Service, my function needs to take in parameters such as File object.

my questions are:

should I mock the input File object?

should I mock the response from the server?

if so, how do i do the mocking? I have already included the mockolate library.

By the way, this is my code for the test so far

public class TestDropInService
{
    private var service:DropInService;
    private var serviceDispatcher:EventDispatcher = new EventDispatcher();

    [Before]
    public function setUp():void
    {
        serviceDispatcher = new EventDispatcher();
        service = new DropInService();
        service.eventDispatcher = serviceDispatcher;
    }

    [After]
    public function tearDown():void
    {
        this.serviceDispatcher = null;
        this.service = null;
    }

    [Test(async)]
    public function testUploadFile():void
    {
        this.serviceDispatcher.addEventListener( UploadFileEvent.SERVER_CONFIRM_UPLOAD_COMPLETE, 
            Async.asyncHandler(this, handleFileUploaded, 8000, null, 
                handleServiceTimeout), false, 0, true);

        this.service.uploadFile(); // this is wrong because my uploadFile expects an File object
    }

    protected function handleServiceTimeout( object:Object ):void
    {
        Assert.fail('Pending Event Never Occurred');
    }

    protected function handleFileUploaded(event:UploadFileEvent, object:Object):void
    {
        Assert.assertEquals("The file upload is successful: ", 
            event.response.valid, true) 
    }
  1. 1 Posted by kimcity on 02 Jun, 2012 03:00 PM

    kimcity's Avatar

    Hi there,

    I hope to get a response regarding writing unit tests.

    Thank you.

  2. 2 Posted by kimcity on 04 Jun, 2012 05:23 PM

    kimcity's Avatar

    Hi there,

    anyone has a clue?

  3. 3 Posted by simon on 06 Jun, 2012 11:07 AM

    simon's Avatar

    The way I have typically tested this in the past is to mock file FileReferenceList for example:

    [Mock] public var fileReferenceList:FileReferenceList;

    I then stub getters on a mock FileReference for example:

    private static const JPG_FILE:String="test.jpg";

    [Mock] public var file:FileReference;

    Which are then stubbed like so:

            stub(file).getter("name").returns(JPG_FILE);
            stub(file).getter("size").returns(1);
            stub(fileReferenceList).getter("fileList").returns([file]);
    

    And here are some example tests:

        [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'));
        }
    

    I hope that helps man,

    Si

  4. Ondina D.F. closed this discussion on 27 Aug, 2012 10:37 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