Search code examples
angulartypescriptangular2-testing

'expect' was used when there was no current spec


I am learning Angular 2 testing and I am getting an error that currently doesn't make sense to me.

'expect' was used when there was no current spec,

Test:

import {ExperimentsComponent} from "./experiments.component";
import {StateService} from "../common/state.service";
import {ExperimentsService} from "../common/experiments.service";

describe('experiments.component title and body should be correct',() => {

  let stateService = StateService;
  let experimentService = ExperimentsService;

  let app = new ExperimentsComponent(new stateService, new experimentService);

  expect(app.title).toBe('Experiments Page');
  expect(app.body).toBe('This is the about experiments body');

});

The component:

import {Component, OnInit} from "@angular/core";
import {Experiment} from "../common/experiment.model";
import {ExperimentsService} from "../common/experiments.service";
import {StateService} from "../common/state.service";


@Component({
    selector: 'experiments',
    template: require('./experiments.component.html'),

})
export class ExperimentsComponent implements OnInit {
    title: string = 'Experiments Page';
    body: string = 'This is the about experiments body';
    message: string;
    experiments: Experiment[];

    constructor(private _stateService: StateService,
                private _experimentsService: ExperimentsService) {
    }

    ngOnInit() {
        this.experiments = this._experimentsService.getExperiments();
        this.message = this._stateService.getMessage();
    }

    updateMessage(m: string): void {
        this._stateService.setMessage(m);
    }
}

Eventually I want to test all the functions in the practice app. But as of right now I am only getting the tests to pass that were generated by angular-cli.

From what I read from the docs it looks correct what I am doing.


Solution

  • expect() statements occur within it() statements like this:

    describe('ExperimentsComponent',() => {
    ...
      it('should be created', () => {
        expect(component).toBeTruthy();
      });
    ...
    }
    

    That's how errors can be read:

    ExperimentsComponent should be created was false

    You seem to have the describe and it parameters confused