在我的控制器中,我正在检查页面是否符合iframed条件:
var vm = this;
if ($window.self !== $window.top) {
//We are iframed
vm.weAreIFramed = true
}
else {
vm.weAreIFramed = false;
}
我想为它编写单元测试。我写了类似的东西,但测试总是失败:
var homeController, $httpBackend, $controller, $location, $rootScope;
var topObj = {};
var selfObj = {};
var windowObj = {
location: { href: '' },
top: topObj,
self: topObj
};
beforeEach(module("app"), function ($provide) {
$provide.value('$window', windowObj);
});
beforeEach(inject(function (_$httpBackend_, _$controller_, _$location_, _$rootScope_) {
$httpBackend = _$httpBackend_;
$controller = _$controller_;
$location = _$location_;
$rootScope = _$rootScope_;
homeController = $controller("homeController");
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("vm.weAreIFramed should be false if we are not iFramed", function () {
console.log(angular.mock.dump(windowObj));
expect(homeController.weAreIFramed).toEqual(false);
});
失败并出现以下错误:
如何编写正确的测试来检查页面是否是iframed?
答案 0 :(得分:0)
测试失败,因为"" === ""
。在真实window
中,top
和self
是对象。
应该是
var topObj = {};
var selfObj = {};
var windowObj = {
location: { href: '' },
top: topObj,
self: selfObj
};
对于其他条件,只能为两者指定topObj
。