我正尝试在一个使用Ionic framework (Angular)的应用程序中测试指令
describe('task lists', function () {
var html, scope, element
beforeEach(function () {
angular.mock.module('app')
})
beforeEach
beforeEach(inject(function ($rootScope, $compile) {
html = angular.element('<ion-item ng-repeat="task in tasks">{{task.taskId}}</ion-item>')
scope = $rootScope.$new()
scope.tasks = [
{ taskId: 1}
, { taskId: 2}
]
element = $compile(html)(scope)
scope.$digest()
}))
it('should list the available tasks', function () {
expect(element.find('li').count()).toBe(2)
console.log(typeof element.find)
})
})但是我得到了这个错误
task lists should list the available tasks.
✘ TypeError: 'undefined' is not a function (evaluating 'element.find('li').count()') in http://localhost:7357/tests/spec/directives/show_task_lists_spec.js (line 22)
http://localhost:7357/tests/spec/directives/show_task_lists_spec.js:22:34
execute@http://localhost:7357/testem/jasmine.js:1064:22
next_@http://localhost:7357/testem/jasmine.js:2096:38
start@http://localhost:7357/testem/jasmine.js:2049:13
execute@http://localhost:7357/testem/jasmine.js:2376:19
next_@http://localhost:7357/testem/jasmine.js:2096:38
start@http://localhost:7357/testem/jasmine.js:2049:13
execute@http://localhost:7357/testem/jasmine.js:2521:19
next_@http://localhost:7357/testem/jasmine.js:2096:38
http://localhost:7357/testem/jasmine.js:2086:23发布于 2014-07-15 23:21:40
导致错误的这一行。
element = $compile(html)(scope)具体来说
$compile(html)当beforeEach在测试前运行时返回为未定义。我想这条线路出了点问题
html = angular.element('<ion-item ng-repeat="task in tasks">{{task.taskId}}</ion-item>')您应该在外部使用双引号,在内部使用单引号。这很可能是你的问题。它应该看起来像这样。
html = angular.element("<ion-item ng-repeat='task in tasks'>{{task.taskId}}</ion-item>")发布于 2014-07-17 00:49:11
问题出在测试用例上。
element.find('li').count()你应该为一个'ion-item‘而不是一个'li’运行find。见下文。
element.find('ion-item').count()https://stackoverflow.com/questions/24329356
复制相似问题