jeudi 2 mai 2019

Karma test error attempting to emulate backend results from service in Angular application

I have a small test service that I am attempting to emulate results for in my integration tests. The service is injected into the constructor of a page and called in the ngOnInit method like so.

import { Component, OnInit } from '@angular/core';
import { TodoService } from './todo.service'

@Component({
  selector: 'app-todos',
  templateUrl: './todos.component.html',
  styleUrls: ['./todos.component.css']
})
export class TodosComponent implements OnInit {
  todos;
  message; 

  constructor(private service: TodoService) {}

  ngOnInit() { 
    this.service.getTodos().subscribe(t => this.todos = t);
  }


I am trying to create an integration test to confirm that the code in the component is written correctly. The service returns an observable of type object array, so I created my test like this.

describe('TodosComponent', () => {
  let component: TodosComponent;
  let fixture: ComponentFixture<TodosComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ HttpClientModule],
      declarations: [ TodosComponent ],
      providers: [TodoService]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TodosComponent);
    component = fixture.componentInstance;
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should load todos from the server', () => {
    let svc = TestBed.get(TodoService);

    let todoData = [
      {id: 1, title: 'todo 1'},
      {id: 2, title: 'todo 2'},
      {id: 3, title: 'todo 3'}
  ];

    spyOn(svc, 'getTodos').and.returnValues(todoData);

    // This statement causes the ngOnInit method to get called
    fixture.detectChanges();

    expect(component.todos.length).toBe(3);
    expect(component.todos).toBe(todoData);
  });
});

When I run this test, I get the following errors: The first error occurs twice - not sure why..

zone.js:3272 Access to XMLHttpRequest at 'ng:///DynamicTestModule/TodosComponent_Host.ngfactory.js' from origin 'http://localhost:9876' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.


TypeError: this.service.getTodos(...).subscribe is not a function
    at TodosComponent../src/app/2-todos/todos.component.ts.TodosComponent.ngOnInit (_karma_webpack_/webpack:/src/app/2-todos/todos.component.ts:16)
    at checkAndUpdateDirectiveInline (_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:22099)
    at checkAndUpdateNodeInline (_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:23363)
    at checkAndUpdateNode (_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:23325)
    at debugCheckAndUpdateNode (_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:23959)
    at debugCheckDirectivesFn (_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:23919)
    at Object.eval [as updateDirectives] (VM723 TodosComponent_Host.ngfactory.js:9)
    at Object.debugUpdateDirectives [as updateDirectives] (_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:23911)
    at checkAndUpdateView (_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:23307)
    at callWithDebugContext (_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:24177)

I am following a tutorial, and their test runs properly. They are using an older version of Angular - 4 I believe. My Karma, Angular are the latest stable version. If someone could help explain how to get around this issue I would be eternally grateful. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire