如何在angularjs中的控制器内为Date方法创建模拟

时间:2013-12-31 08:01:49

标签: javascript unit-testing angularjs jasmine

$scope.deleteAbc = ->
  alert("xyz")

当从test测试此方法时,如何获取警报的值。

describe "XYZ", ->
  $scope = null
  $controller  = null
  beforeEach inject ($rootScope,$controller) ->
    $scope = $rootScope
    ctrl = $controller("StatusController",
      $scope: $scope
    )

1 个答案:

答案 0 :(得分:0)

您可以将AngularJS的$窗口注入控制器并调用$ window.alert(),而不是调用alert()。

您可以在测试中伪造$ window对象。警报变量包含警报消息。

describe "XYZ", ->
  $scope = null
  $controller  = null
  alerted = null
  beforeEach inject ($rootScope,$controller) ->
    $scope = $rootScope
    $window:
      alert: (message) ->
        alerted = message
    ctrl = $controller("StatusController",
      $scope: $scope
      $window: $window
    )
相关问题