Jasmine AngularJS - 如何模拟 window.user.username
Jasmine AngularJS - How to Mock window.user.username
我是一名 Jasmine 菜鸟,正在尝试弄清楚如何在使用 Jasmine 测试 angularjs 时模拟 window.user.username 对象。
var applicationUser = window.user.username;
我只是想知道,不确定,这是否是正确的方法。但它奏效了。希望对你有帮助。
//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
});
};
另一种方法,
您必须将用户对象添加到 jasmine 全局 space。
//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
});
};
我是一名 Jasmine 菜鸟,正在尝试弄清楚如何在使用 Jasmine 测试 angularjs 时模拟 window.user.username 对象。
var applicationUser = window.user.username;
我只是想知道,不确定,这是否是正确的方法。但它奏效了。希望对你有帮助。
//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
});
};
另一种方法,
您必须将用户对象添加到 jasmine 全局 space。
//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
});
};