问题与NgModel

时间:2017-07-18 13:10:42

标签: angular testing angular-ngmodel

我尝试测试具有NgModel且测试用例有错误的Component。任何指针都会有所帮助。

TypeError: Cannot create property '__creationTrace__' on string '__zone_symbol__optimizedZoneEventTask'
    at Object.onScheduleTask (http://localhost:9876/_karma_webpack_/vendor.bundle.js:3636:38)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:2814:51)
    at Object.onScheduleTask (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:2710:29)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:2814:51)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleTask (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:2645:43)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleEventTask (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:2671:25)
    at HTMLInputElement.addEventListener (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:3971:29)
    at DomEventsPlugin.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DomEventsPlugin.addEventListener (http://localhost:9876/_karma_webpack_/vendor.bundle.js:61953:17)
    at EventManager.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.EventManager.addEventListener (http://localhost:9876/_karma_webpack_/vendor.bundle.js:61291:23)
    at EmulatedEncapsulationDomRenderer2.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DefaultDomRenderer2.listen (http://localhost:9876/_karma_webpack_/vendor.bundle.js:61812:36)

测试文件:

import { TestBed, async } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { Todo } from './todo'

describe('AppComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [ 
        FormsModule 
      ],
      declarations: [ AppComponent ]
    });
  });

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ 
        FormsModule 
      ],
      declarations: [
        AppComponent
      ]
    });
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

});

组件文件:

import { Component } from '@angular/core';
import { TodoDataService } from './todo-data.service';
import { Todo } from './todo';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [TodoDataService]
})
export class AppComponent {
  newTodo: Todo = new Todo();
  constructor(private todoDataService: TodoDataService) {

  }

  addTodo() {
    this.todoDataService.addTodo(this.newTodo);
    this.newTodo = new Todo();
  }

  toggleTodoComplete(todo) {
    this.todoDataService.toggleTodoComplete(todo);
  }

  removeTodo(todo) {
    this.todoDataService.deleteTodoById(todo.id);
  }

  get todos() {
    return this.todoDataService.getAllTodos();
  }
}

模板文件:

<section class="todoapp">
  <header class="header">
    <h1>Todos</h1>
    <input class="new-todo" placeholder="What needs to be done?" autofocus="" [(ngModel)]="newTodo.title"
     (keyup.enter)="addTodo()">
  </header>
  <section class="main" *ngIf="todos.length > 0">
    <ul class="todo-list">
      <li *ngFor="let todo of todos" [class.completed]="todo.complete">
        <div class="view">
          <input class="toggle" type="checkbox" (click)="toggleTodoComplete(todo)" [checked]="todo.complete">
          <label>{{todo.title}}</label>
          <button class="destroy" (click)="removeTodo(todo)"></button>
        </div>
      </li>
    </ul>
  </section>
  <footer class="footer" *ngIf="todos.length > 0">
    <span class="todo-count"><strong>{{todos.length}}</strong> {{todos.length == 1 ? 'item' : 'items'}} left</span>
  </footer>
</section>

的package.json

{
  "name": "my-app",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.2.1",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
}

0 个答案:

没有答案