在尝试对我的自定义指令进行单元测试时,我遇到了一个问题,即根据find
指令中的表达式尝试ng-class
一个应用了动态类的元素。在我看来,表达根本就没有评估。这有可能测试吗?如果是这样,我做错了什么或错过了什么?
第一次单元测试通过正常,第二次是问题所在。
测试:
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
element = '<tp-navigation-bar></tp-navigation-bar>';
scope.currentPage = 'reports';
scope.contactName = 'Test Name';
scope.isLoggedIn = true;
element = $compile(element)(scope);
scope.$digest();
}));
it("should contain a dropdown with the contact name as the title", function () {
expect(element.find('.dropdown-toggle span:nth-child(2)').text()).toEqual('Test Name');
});
it("should have reports set as the active page", function () {
expect(element.find('li .active').text()).toEqual('Reports');
});
模板:
<ul class="nav navbar-nav navbar-right" ng-show="isLoggedIn">
<li ng-class="{active:currentPage === 'mainDashboard'}">
Home
</li>
<li ng-class="{active:currentPage === 'requests'}">
Requests
</li>
<li ng-class="{active:currentPage === 'reports'}">
Reports
</li>
</ul>
答案 0 :(得分:1)
Selector is incorrect. You have a space between li
and .active
which implies that the class is a descendant of the li
.
Try removing the space
element.find('li.active')