茉莉花unit testing元素不是:隐藏
我有一个AngularJS指令,我正在编写测试。 我已经在这个问题中重新创建了这个问题: http ://plnkr.co/edit/gHoGwI6TZhsgKVwIb1uI? p = preview
任何人都可以告诉我为什么第二次测试失败以及如何解决它?
码:
var app = angular.module("myApp", []); app.directive("myDirective", function() { return { template: "Hello this is my directive!" } }); describe("Testing hidden", function() { var $compile, $scope, $document; beforeEach(inject(function(_$compile_, $rootScope, _$document_){ $compile = _$compile_; $scope = $rootScope.$new(); $document = _$document_; })); function getElement() { var element = angular.element(""); $compile(element)($scope); return element; } it("passes", function() { var element = getElement(); expect(element.is(":hidden")).toBe(true); // Passes }); it("fails", function() { var element = getElement(); $document.body.append(element); element.show(); expect(element.is(":hidden")).toBe(false); // Fails }); });
您需要将您的元素添加到DOM;
我已经使用快速解决方案更新了您的代码。 当然,您可以将它添加到beforeEach中的DOM中,并在afterEach中将其从DOM中删除。