2013-07-17 38 views
0

试图让我的头脑模糊,在我的项目中实现一些单元测试。我想测试我的MainMediator,并且因为在我的MainMediator onRegister调用中创建了一些对象,所以我想我应该嘲笑这些对象。 希望这是正确的开始!asmock以前的方法需要返回值或抛出的异常

我有这样的事情

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

当我一步通过测试,并在mockRepository.stubEvents(调度员)停止。我可以在myService类 Error: Previous method IMyService/clientID/get(); requires a return value or an exception to throw中看到错误。 clientID恰好是我的第一个属性,因此它被选中。

我以为要么StubOptions.NONE将意味着没有属性被残留或我的SetupResult.forCall(myService.clientID)会解决它,但没有。

回答这个问题的评论回复:在此事件,我有:

MyService extends ServiceBase implements IMyService

其中ServiceBase extends Actor

我发现我需要在IMyService下列以获得访问此事件。

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

不太确定这是否正确。现在有点困惑。

能有人告诉我我要去哪里错了吗? 谢谢!

+0

“myService eventDispatcher”从哪里来? –

回答

0

当模拟具体类而不是接口时,这是一个常见问题:如果构造函数调用另一个方法(或属性getter),它将返回null,因为它尚未被模拟。

解决这个问题并不是真的,除了通过接口抽象你的类并模拟

+0

感谢您的回答!不幸的是我在嘲笑界面后仍然面临同样的问题。我在上面编辑了我的问题,并添加了一些可能有助于解决问题的信息。 –

+0

您的变量现在输入为“IMyService”,但您仍然将'MyService'传递给'createStub',所以它仍然试图创建具体类的模拟。 –

+0

对不起,这是我的一个错字。我的确在传递IMyService。 –