Angularjs ng类在单元测试期间未进行评估
在尝试对我的自定义指令进行单元测试时,我遇到了一个问题,即根据Angularjs ng类在单元测试期间未进行评估,angularjs,unit-testing,angularjs-directive,Angularjs,Unit Testing,Angularjs Directive,在尝试对我的自定义指令进行单元测试时,我遇到了一个问题,即根据ng class指令中的表达式,试图查找应用了动态类的元素。在我看来,这个表达式根本不是在求值。这可以测试吗?如果是这样,我做错了什么或错过了什么 第一个单元测试顺利通过,第二个就是问题所在 测试: beforeEach(inject(function ($rootScope, $compile) { scope = $rootScope.$new(); element = '<tp-navigation-b
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');
});
beforeach(注入(函数($rootScope,$compile){
scope=$rootScope.$new();
元素=“”;
scope.currentPage='报告';
scope.contactName='测试名称';
scope.isLoggedIn=true;
元素=$compile(元素)(范围);
范围。$digest();
}));
它(“应该包含一个以联系人姓名为标题的下拉列表”,函数(){
expect(element.find('.dropdown-toggle-span:nth-child(2)').text()).toEqual('testname');
});
它(“应将报告设置为活动页面”,函数(){
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>
-
家
-
请求
-
报告
选择器不正确。在li
和.active
之间有一个空格,这意味着类是li
的后代
尝试删除空间
element.find('li.active')
非常感谢。我快疯了。