Installation
From asmock
Contents |
Quick Start Table Of Contents
- Introduction
- Usage
- Installation
- Feature Overview
- Method Arguments
- Stubbing Properties
- Stubbing Events
- Expectations
- Constraints
- Ordered Calls
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.
- Download ASMock
- Add ASMock.swc to your library path
- Add ASMockFlexUnit4.swc to your library path
- Add [RunWith("asmock.integration.flexunit.ASMockClassRunner")] to your test class
- Add [Mock("class.to.Mock")] to your test class or test methods (you can add multiple [Mock] items)
- 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!)
- Download ASMock
- Download the fluint Integration Pack
- Add ASMock.swc to your library path
- Add ASMockFluint1.swc to your library path
- Create a test class that extends MockTestCase
- 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!)
- Download ASMock
- Download the Flex Unit 1 Integration Pack
- Add ASMock.swc to your library path
- Add ASMockFlexUnit1.swc to your library path
- Create a test class that extends ASMockTestCase
- 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:
- Download ASMock
- 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!
}
}
