0

Trying to get my head around asmock to implement some unit testing in my project. I want to test my MainMediator and since there are objects that get created in my MainMediator onRegister call, I'm thinking that I should mock those objects. Hopefully that's correct to begin with!

I have something like this

 [Rule] public var includeMocks : IncludeMocksRule = new IncludeMocksRule([
    IEventDispatcher, IMyService
 ]);

 [Before]
 public function setUp():void {
    mockRepository = new MockRepository();
    mainView = new MainView();
    mainMediator = new MainMediator();

    dispatcher = IEventDispatcher(mockRepository.createStub(IEventDispatcher, StubOptions.NONE));
    myService = IMyService(mockRepository.createStub(IMyService, StubOptions.NONE));
    mockRepository.stubEvents(dispatcher);

    SetupResult.forCall(chatService.clientID)
              .returnValue("");

    mockRepository.replayAll();

    mainMediator.eventDispatcher = dispatcher;
    myService.eventDispatcher = dispatcher;   
    mainMediator.service = myService;  

    ....

    mainMediator.onRegister();
 }

When I step through the test and stop at mockRepository.stubEvents(dispatcher). I can see errors in the myService class Error: Previous method IMyService/clientID/get(); requires a return value or an exception to throw. clientID just happens to be my first property hence why it's being picked on.

I thought either that StubOptions.NONE would mean that no properties get stubbed or that my SetupResult.forCall(myService.clientID) would fix it but none did.

Answering to the question in the comment re: the eventDispatcher, I have:

MyService extends ServiceBase implements IMyService

where ServiceBase extends Actor

I found that I need the following in IMyService to get access to the eventDispatcher.

function get eventDispatcher():IEventDispatcher;
function set eventDispatcher(dispatcher:IEventDispatcher):void;

Not too sure if that is correct. Bit confused now.

Can someone please tell me where I'm going wrong? Thanks!

m.y
  • 691
  • 9
  • 21

1 Answers1

0

This is a common problem when mocking concrete classes, rather than interfaces: if the constructor calls another method (or property getter), it will return null because it hasn't been mocked yet.

There's not really anyway to workaround it, except to abstract your class through an interface and mock that.

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • Thanks for your answer! Unfortunately I'm still facing the same problem after mocking the interface. I've edited my question above and added some info that might help shed some light. – m.y Jul 18 '13 at 14:05
  • Your variables are now typed as `IMyService`, but you are still passing `MyService` into `createStub`, so it's still trying to create a mock of the concrete class. – Richard Szalay Jul 20 '13 at 10:33
  • Sorry that was a typo on my part. I am indeed passing IMyService. – m.y Jul 22 '13 at 08:21