我有下面的代碼,我想盡可能多地測試它。我只是在摸索着如何在authenticationService.authenticate()
的當時塊內測試這兩條線。我已經來回搜索,但我還沒有找到任何可用的信息。如何在茉莉花承諾之後測試代碼中的代碼?
當我檢查廣播是否被觸發時,結果是錯誤的,因爲最初有廣播消息觸發整個代碼。
function shellController($location, $rootScope, $scope, authenticationService, authService, common)
{
$scope.$on('event:auth-loginRequired', function()
{
var user = {
userName: 'visitor',
password: 'visitor'
};
authenticationService.authenticate(user).then(function (result) {
$rootScope.$broadcast('event:username-changed');
authService.loginConfirmed();
});
});
}
我如何可以模擬當authenticationService.authenticate()
成功返回 - 它不是測試對象是否成功與否返回 - 而$rootScope.$broadcast('event:username-changed');
是否被調用 - 這是測試對象?
你只是想確認是否'事件:用戶名 - change'事件有被廣播和authService.loginConfirmed()被調用? – ryeballar
是的。那是對的! – SayusiAndo