Jasmine AngularJS - 如何模拟window.user.username

时间:2015-01-27 19:46:37

标签: angularjs jasmine

我是一名Jasmine新手,在使用Jasmine测试angularjs时试图弄清楚如何模拟window.user.username对象。

var applicationUser = window.user.username;

3 个答案:

答案 0 :(得分:0)

我只是想通了,不确定,如果这是正确的方法。但它奏效了。希望它有所帮助。



//Main Function
function MyController($scope, $window) {
    $scope.HeaderCtrl = function() {

      $scope.user = $window.user;
      ---

    }


    //Jasmine Code
    var $window;
    beforeEach(inject(function(_$window_) {
      $window = _$window_;
      $window.user = 'xyz';
    }));

    createController = function() {
      return $controller('EpicController', {
        '$scope': $rootScope,
        '$window': $window
      });
    };




答案 1 :(得分:0)

答案 2 :(得分:0)

您必须将用户对象添加到jasmine全局空间。



//Main Function
function MyController($scope, $window) {
    $scope.HeaderCtrl = function() {

      $scope.user = $window.user;
      ---

    }


    //Jasmine Code
    var $window;
    beforeEach(inject(function(_$window_) {
      jasmine.getGlobal().$window = _$window_;
       // OR
       // here i am assuming that user class has only username property
      jasmine.getGlobal().$windows = { "user": {"username": "xyz"}}; 
      $window.user = 'xyz';
    }));

    createController = function() {
      return $controller('EpicController', {
        '$scope': $rootScope,
        '$window': $window
      });
    };