如何使用 jasmine 测试 window.location.href
How to test window.location.href using jasmine
这是我的控制器代码
$scope.loadApplications = function () {
var cacheKey = { key: cacheKeys.appList() };
dataFactory.get("/Application/All", { cache: cacheKey })
.then(function (result) {
$scope.count++;
$scope.applications = result.data.data;
$scope.filteredApplicationsList = $scope.applications;
if ($scope.applications.length===0){
$window.location.href = '/Account/WelCome'
}
else {
$scope.isLoad = true;
}
});
};
这是我对上述功能的 jasmine 测试用例
it('should redirect to welcome page', function () {
scope.applications = {};
scope.loadApplications();
expect(window.location.href).toContain('/Account/WelCome');
});
但它正在使用当前的 url 浏览器并且它引发了一个错误
Expected 'http://localhost:49363/Scripts/app/test/SpecRunner.html' to contain '/Account/WelCome'.
谁能告诉我如何测试 url?
你应该这样写。
it('should redirect to welcome page', function () {
scope.applications = {};
scope.loadApplications();
browser.get('http://localhost:49363/Account/WelCome');
expect(browser.getCurrentUrl()).toEqual('whateverbrowseryouarexpectingtobein');
expect(browser.getCurrentUrl()).toContain('whateverbrowseryouarexpectingtobein');
//expect(window.location.href).toContain('/Account/WelCome');
});
最好使用 $windows 服务而不是 window 对象,因为它是一个全局对象。 AngularJS 始终通过 $window 服务引用它,因此它可能会被覆盖、删除或模拟以进行测试。
describe('unit test for load application', function(){
beforeEach(module('app', function ($provide) {
$provide.value('$window', {
location: {
href: ''
}
});
}));
it('should redirect to welcome page', function ($window) {
scope.applications = {};
scope.loadApplications();
expect($window.location.href).toContain('/Account/WelCome');
});
});
这是我的控制器代码
$scope.loadApplications = function () {
var cacheKey = { key: cacheKeys.appList() };
dataFactory.get("/Application/All", { cache: cacheKey })
.then(function (result) {
$scope.count++;
$scope.applications = result.data.data;
$scope.filteredApplicationsList = $scope.applications;
if ($scope.applications.length===0){
$window.location.href = '/Account/WelCome'
}
else {
$scope.isLoad = true;
}
});
};
这是我对上述功能的 jasmine 测试用例
it('should redirect to welcome page', function () {
scope.applications = {};
scope.loadApplications();
expect(window.location.href).toContain('/Account/WelCome');
});
但它正在使用当前的 url 浏览器并且它引发了一个错误
Expected 'http://localhost:49363/Scripts/app/test/SpecRunner.html' to contain '/Account/WelCome'.
谁能告诉我如何测试 url?
你应该这样写。
it('should redirect to welcome page', function () {
scope.applications = {};
scope.loadApplications();
browser.get('http://localhost:49363/Account/WelCome');
expect(browser.getCurrentUrl()).toEqual('whateverbrowseryouarexpectingtobein');
expect(browser.getCurrentUrl()).toContain('whateverbrowseryouarexpectingtobein');
//expect(window.location.href).toContain('/Account/WelCome');
});
最好使用 $windows 服务而不是 window 对象,因为它是一个全局对象。 AngularJS 始终通过 $window 服务引用它,因此它可能会被覆盖、删除或模拟以进行测试。
describe('unit test for load application', function(){
beforeEach(module('app', function ($provide) {
$provide.value('$window', {
location: {
href: ''
}
});
}));
it('should redirect to welcome page', function ($window) {
scope.applications = {};
scope.loadApplications();
expect($window.location.href).toContain('/Account/WelCome');
});
});