Angular10:服务单元测试"spec has no expectations"
Angular 10: Unit Test of service "spec has no expectations"
我有一个非常简单的服务,我想对其进行单元测试。
import { Injectable } from '@angular/core';
import { Step } from './step';
@Injectable({
providedIn: 'root'
})
export class StepService {
constructor() { }
setStepDone(setDoneStep: Step) {
setDoneStep.done = !setDoneStep.done;
}
}
我的测试是这样的:
import { TestBed } from '@angular/core/testing';
import { StepService } from './step.service';
import { Step } from './step';
describe('StepService', () => {
let service: StepService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(StepService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should set the done property of a step to true when done was false and setStepDone was called once', () => {
let step: Step = { description: "test", done: false };
service.setStepDone(step);
expect(step.done).toBeTrue;
});
it('should set the done property of a step to false when done was false and setStepDone was called twice', (done) => {
let step: Step = { description: "test", done: false };
service.setStepDone(step);
service.setStepDone(step);
expect(step.done).toBeFalse;
done();
});
});
不幸的是,在执行测试时它说:
StepService
should be created
SPEC HAS NO EXPECTATIONS should set the done property of a step to true when done was false and setStepDone was called once
SPEC HAS NO EXPECTATIONS should set the done property of a step to false when done was false and setStepDone was called twice
所以“应该创建”似乎有效,但其他两个没有期望,虽然我期望 step.done
是真还是假。
为什么它不能识别 expect(...)
语句?
提前致谢
tobeTrue是一个函数,所以你需要加括号才能正确调用它(与toBeFalse相同)
expect(step.done).toBeTrue();
测试现在将找到可测试的期望值。
我有一个非常简单的服务,我想对其进行单元测试。
import { Injectable } from '@angular/core';
import { Step } from './step';
@Injectable({
providedIn: 'root'
})
export class StepService {
constructor() { }
setStepDone(setDoneStep: Step) {
setDoneStep.done = !setDoneStep.done;
}
}
我的测试是这样的:
import { TestBed } from '@angular/core/testing';
import { StepService } from './step.service';
import { Step } from './step';
describe('StepService', () => {
let service: StepService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(StepService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should set the done property of a step to true when done was false and setStepDone was called once', () => {
let step: Step = { description: "test", done: false };
service.setStepDone(step);
expect(step.done).toBeTrue;
});
it('should set the done property of a step to false when done was false and setStepDone was called twice', (done) => {
let step: Step = { description: "test", done: false };
service.setStepDone(step);
service.setStepDone(step);
expect(step.done).toBeFalse;
done();
});
});
不幸的是,在执行测试时它说:
StepService should be created
SPEC HAS NO EXPECTATIONS should set the done property of a step to true when done was false and setStepDone was called once
SPEC HAS NO EXPECTATIONS should set the done property of a step to false when done was false and setStepDone was called twice
所以“应该创建”似乎有效,但其他两个没有期望,虽然我期望 step.done
是真还是假。
为什么它不能识别 expect(...)
语句?
提前致谢
tobeTrue是一个函数,所以你需要加括号才能正确调用它(与toBeFalse相同)
expect(step.done).toBeTrue();
测试现在将找到可测试的期望值。