Welcome, Guest! Log In | Create Account

Installation

From asmock

Jump to: navigation, search

Contents

Quick Start Table Of Contents

Flex Unit 4

Flex Unit 4 is the best way to use ASMock as it doesn't require a base class and works entirely from metadata.

  1. Download ASMock
  2. Add ASMock.swc to your library path
  3. Add ASMockFlexUnit4.swc to your library path
  4. Add [RunWith("asmock.integration.flexunit.ASMockClassRunner")] to your test class
  5. Add [Mock("class.to.Mock")] to your test class or test methods (you can add multiple [Mock] items)
  6. Add the following to the "Additional compiler arguments" under "Flex Compiler" in your project properties:
-includes asmock.integration.flexunit.ASMockClassRunner

Below is a complete example:

[RunWith("asmock.integration.flexunit.ASMockClassRunner")]
public class Sample
{
    [Test]
    [Mock("org.company.IShoppingCartRepository")]
    public function testCanCheckOut_ItemCountGreaterThanZero() : void
    {
        var mockRepository : MockRepository = new MockRepository();
    
        // Record
        var shoppingCartRepos : IShoppingCartRepository = 
            IShoppingCartRepository(mockRepsitory.createStub(IShoppingCartRepository));

        SetupResult.forCall(shoppingCartRepos.getItemCount()).returnValue(5);
    
        // Replay
        mockRepository.replay(shoppingCartRepos);

        trace(shoppingCartRepos.getItemCount());
    }
}

fluint

(fluint support will be distributed in SWC form in an upcoming release, stay tuned!)

  1. Download ASMock
  2. Download the fluint Integration Pack
  3. Add ASMock.swc to your library path
  4. Add ASMockFluint1.swc to your library path
  5. Create a test class that extends MockTestCase
  6. Pass an array of classes you need mocked to the base constructor

Below is a complete example:

public class SampleTestCase extends MockTestCase
{
    public SampleTestCase()
    {
        super([IShoppingCartRepository]);
    }

    public function test_itemCount_returnsFive() : void
    {
        var mockRepsitory : MockRepository = new MockRepository();

        // Record
        var shoppingCartRepos : IShoppingCartRepository = 
            IShoppingCartRepository(mockRepsitory.createStub(IShoppingCartRepository));

        SetupResult.forCall(shoppingCartRepos.getItemCount()).returnValue(5);
    
        // Replay
        mockRepository.replay(shoppingCartRepos);

        trace(shoppingCartRepos.getItemCount());
    }
}

Flex Unit 1

(Flex Unit 1 support will be distributed in SWC form in an upcoming release, stay tuned!)

  1. Download ASMock
  2. Download the Flex Unit 1 Integration Pack
  3. Add ASMock.swc to your library path
  4. Add ASMockFlexUnit1.swc to your library path
  5. Create a test class that extends ASMockTestCase
  6. Pass an array of classes you need mocked to the base constructor

Below is a complete example:

public class SampleTestCase extends ASMockTestCase
{
    public SampleTestCase()
    {
        super([IShoppingCartRepository]);
    }

    public function test_itemCount_returnsFive() : void
    {
        var mockRepsitory : MockRepository = new MockRepository();

        // Record
        var shoppingCartRepos : IShoppingCartRepository = 
            IShoppingCartRepository(mockRepsitory.createStub(IShoppingCartRepository));

        SetupResult.forCall(shoppingCartRepos.getItemCount()).returnValue(5);
    
        // Replay
        mockRepository.replay(shoppingCartRepos);

        trace(shoppingCartRepos.getItemCount());
    }
}

Stand Alone

There's nothing stopping you using ASMock by itself, but it take's a few extra lines of code. This is because the mocking process is actually asynchronous (due to some technical limitations in Flash) and the integration frameworks doing the work for you.

First, let's get our Flex project ready:

  1. Download ASMock
  2. Add ASMock.swc to your library path

Now, let's write a sample:

public class ASMockExample
{
    private var _repository : MockRepository;

    public ASMockExample()
    {
        _repository = new MockRepository();
    }

    public function testMock() : void
    {
        var prepareDispatcher : IEventDispatcher = _repository.prepare([IShoppingCartRepository]);
        prepareDispatcher.addEventListener(Event.COMPLETE, mockComplete);
    }

    private function mockComplete() : void
    {
        var shoppingCart : IShoppingCartRepository = 
            IShoppingCartRepository(repository.createStub(IShoppingCartRepository));

        // Done!
    }
}

External Links