简体   繁体   English

Ionic框架中的测试指令

[英]Testing directives in Ionic framework

I am trying to test directives in an app that uses Ionic framework (Angular) on a Testem server 我正在尝试在Testem服务器上使用Ionic框架(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)
    })
})

But I am getting this error 但是我收到这个错误

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

This line causing the error. 这行导致错误。

element = $compile(html)(scope)

Specifically 特别

$compile(html)

Is coming back as undefined when the beforeEach runs before the test. 当beforeEach在测试之前运行时,返回未定义状态。 I believe there is something wrong with this line 我相信这条线有问题

html = angular.element('<ion-item ng-repeat="task in tasks">{{task.taskId}}</ion-item>')

You should use double quotes on the outside and single quotes on the inside. 您应该在外部使用双引号,在内部使用单引号。 That is most likely your problem. 那很可能是您的问题。 It should look like this. 它应该看起来像这样。

html = angular.element("<ion-item ng-repeat='task in tasks'>{{task.taskId}}</ion-item>")

The problem is with your test case. 问题出在您的测试用例上。

element.find('li').count()

You should run find for an 'ion-item' instead of a 'li'. 您应该为“离子项目”而不是“ li”运行find。 See below. 见下文。

element.find('ion-item').count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM