使用setup函数替代beforeEach函数进行Angular单元测试
生活随笔
收集整理的這篇文章主要介紹了
使用setup函数替代beforeEach函数进行Angular单元测试
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
具有依賴關(guān)系的Service源代碼:
import { Injectable } from '@angular/core';@Injectable() export class MasterService {constructor(private valueService: ValueService) { }getValue() { return this.valueService.getValue(); } }export class ValueService {getValue() { return 'Jerry'; } }單元測試代碼:
describe('MasterService using setup Instead of beforeEach', () => {it('#getValue should return stubbed value from a spy', () => {const { masterService, stubValue, valueServiceSpy } = setup();expect(masterService.getValue()).toBe(stubValue, 'service returned stub value');expect(valueServiceSpy.getValue.calls.count()).toBe(1, 'spy method was called once');expect(valueServiceSpy.getValue.calls.mostRecent().returnValue).toBe(stubValue);}); });測試結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的使用setup函数替代beforeEach函数进行Angular单元测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全球首批机器受精试管婴儿出生,使用 PS
- 下一篇: 10.18万起 比亚迪驱逐舰05冠军版上