Angular 用茉莉花嘲弄天使9服务
使用Angular 7,我可以通过创建几个类来模仿Jasmine的Angular 用茉莉花嘲弄天使9服务,angular,typescript,unit-testing,jasmine,angular9,Angular,Typescript,Unit Testing,Jasmine,Angular9,使用Angular 7,我可以通过创建几个类来模仿Jasmine的搜索服务。第一个是一个helper.ts文件,它有一个可以扩展的类 /// <reference path="../../../../../node_modules/@types/jasmine/index.d.ts"/> export interface GuinessCompatibleSpy extends jasmine.Spy { /** By chaining the spy with and.r
搜索服务。第一个是一个helper.ts
文件,它有一个可以扩展的类
/// <reference path="../../../../../node_modules/@types/jasmine/index.d.ts"/>
export interface GuinessCompatibleSpy extends jasmine.Spy {
/** By chaining the spy with and.returnValue, all calls to the function will return a specific
* value. */
andReturn(val: any): void;
/** By chaining the spy with and.callFake, all calls to the spy will delegate to the supplied
* function. */
andCallFake(fn: Function): GuinessCompatibleSpy;
/** removes all recorded calls */
reset();
}
export class SpyObject {
static stub(object = null, config = null, overrides = null) {
if (!(object instanceof SpyObject)) {
overrides = config;
config = object;
object = new SpyObject();
}
const m = {};
Object.keys(config).forEach((key) => m[key] = config[key]);
Object.keys(overrides).forEach((key) => m[key] = overrides[key]);
for (const key in m) {
object.spy(key).andReturn(m[key]);
}
return object;
}
constructor(type = null) {
if (type) {
for (const prop in type.prototype) {
let m = null;
try {
m = type.prototype[prop];
} catch (e) {
// As we are creating spys for abstract classes,
// these classes might have getters that throw when they are accessed.
// As we are only auto creating spys for methods, this
// should not matter.
}
if (typeof m === 'function') {
this.spy(prop);
}
}
}
}
spy(name) {
if (!this[name]) {
this[name] = this._createGuinnessCompatibleSpy(name);
}
return this[name];
}
prop(name, value) { this[name] = value; }
/** @internal */
_createGuinnessCompatibleSpy(name): GuinessCompatibleSpy {
const newSpy: GuinessCompatibleSpy = <any>jasmine.createSpy(name);
newSpy.andCallFake = <any>newSpy.and.callFake;
newSpy.andReturn = <any>newSpy.and.returnValue;
newSpy.reset = <any>newSpy.calls.reset;
// revisit return null here (previously needed for rtts_assert).
newSpy.and.returnValue(null);
return newSpy;
}
}
这是我的search.service.mock.ts
:
import { SpyObject } from './helper';
import { SearchService } from '../search.service';
import Spy = jasmine.Spy;
export class MockSearchService extends SpyObject {
getAllSpy: Spy;
getByIdSpy: Spy;
searchSpy: Spy;
saveSpy: Spy;
fakeResponse: any;
constructor() {
super(SearchService);
this.fakeResponse = null;
this.getAllSpy = this.spy('getAll').andReturn(this);
this.getByIdSpy = this.spy('get').andReturn(this);
this.searchSpy = this.spy('search').andReturn(this);
this.saveSpy = this.spy('save').andReturn(this);
}
subscribe(callback: any) {
callback(this.fakeResponse);
}
setResponse(json: any): void {
this.fakeResponse = json;
}
}
然后我在测试中模仿它
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SearchComponent } from './search.component';
import { MockSearchService } from '../shared/search/mocks/search.service';
import { MockActivatedRoute, MockRouter } from '../shared/search/mocks/routes';
import { SearchService } from '../shared';
import { ActivatedRoute, Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { FormsModule } from '@angular/forms';
describe('SearchComponent', () => {
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;
let mockSearchService: MockSearchService;
let mockActivatedRoute: MockActivatedRoute;
beforeEach(async(() => {
mockSearchService = new MockSearchService();
mockActivatedRoute = new MockActivatedRoute({'term': 'peyton'});
TestBed.configureTestingModule({
declarations: [ SearchComponent ],
providers: [
{provide: SearchService, useValue: mockSearchService},
{provide: ActivatedRoute, useValue: mockActivatedRoute}
],
imports: [FormsModule, RouterTestingModule]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
然后我得到两个错误:
Chrome 78.0.3904 (Mac OS X 10.15.1) SearchComponent should create FAILED
Failed: this.getSpy is not a function
at <Jasmine>
知道为什么这在Angular 7中有效而在Angular 9中无效吗
与Angular 7配合使用的应用程序位于GitHub上。我决定更容易摆脱helper.ts,并模拟服务返回的内容。我还更改为导入HttpClientTestingModule
,这样就可以实例化服务,即使从未使用过它的HttpClient
。这是重构后我的search.component.spec.ts
:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SearchComponent } from './search.component';
import { MockActivatedRoute } from '../shared/search/mocks/routes';
import { SearchService } from '../shared';
import { ActivatedRoute } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs';
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('SearchComponent', () => {
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;
let mockActivatedRoute: MockActivatedRoute;
let mockSearchService: SearchService;
beforeEach(async(() => {
mockActivatedRoute = new MockActivatedRoute({term: 'nikola'});
TestBed.configureTestingModule({
declarations: [SearchComponent],
providers: [
{provide: ActivatedRoute, useValue: mockActivatedRoute}
],
imports: [FormsModule, RouterTestingModule, HttpClientTestingModule]
}).compileComponents();
}));
beforeEach(() => {
// mock response
mockSearchService = TestBed.inject(SearchService);
mockSearchService.search = jasmine.createSpy().and.returnValue(of([]));
// initialize component
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should search when a term is set and search() is called', () => {
component = fixture.debugElement.componentInstance;
component.query = 'J';
component.search();
expect(mockSearchService.search).toHaveBeenCalledWith('J');
});
it('should search automatically when a term is on the URL', () => {
fixture.detectChanges();
expect(mockSearchService.search).toHaveBeenCalledWith('nikola');
});
});
在spec.ts中有NullInjectorError:R3InjectorError,在8中运行良好。该测试将创建一个mock@Injectable服务
import { EditComponent } from './edit.component';
import { TestBed } from '@angular/core/testing';
import { Address, Person, SearchService } from '../shared';
import { MockRouter, MockActivatedRoute } from '../shared/search/mocks/routes';
import { ActivatedRoute, Router } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { of } from 'rxjs';
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('EditComponent', () => {
let mockSearchService: SearchService;
let mockActivatedRoute: MockActivatedRoute;
let mockRouter: MockRouter;
beforeEach(() => {
mockActivatedRoute = new MockActivatedRoute({id: 1});
mockRouter = new MockRouter();
TestBed.configureTestingModule({
declarations: [EditComponent],
providers: [
{provide: ActivatedRoute, useValue: mockActivatedRoute},
{provide: Router, useValue: mockRouter}
],
imports: [FormsModule, HttpClientTestingModule]
}).compileComponents();
mockSearchService = TestBed.inject(SearchService);
});
it('should fetch a single record', () => {
const fixture = TestBed.createComponent(EditComponent);
const person = new Person({id: 1, name: 'Gary Harris'});
person.address = new Address({city: 'Denver'});
// mock response
spyOn(mockSearchService, 'get').and.returnValue(of(person));
// initialize component
fixture.detectChanges();
// verify service was called
expect(mockSearchService.get).toHaveBeenCalledWith(1);
// verify data was set on component when initialized
const editComponent = fixture.debugElement.componentInstance;
expect(editComponent.editAddress.city).toBe('Denver');
// verify HTML renders as expected
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h3').innerHTML).toBe('Gary Harris');
});
});
在Angular 9中,堆栈跟踪显示Ivy编译器正在尝试创建真正的服务,最终失败,因为该服务具有进一步的依赖关系
作为临时解决办法,禁用常春藤可以解决此问题
"angularCompilerOptions": {
"enableIvy": false
}
(请注意,这可能需要在tsconfig.json或tsconfig.spec.json中设置,具体取决于您的设置)为什么您要自己创建模拟,而不是使用:{provide:SearchService,useClass:MockSearchService}
和路由测试模块
我需要自己设置,以便稍后在测试中参考它。例如:expect(mockSearchService.searchSpy).toHaveBeenCalledWith('M')代码>当你从测试床中取出它们时,你也可以这样做:constsomemock=TestBed.get(SearchService)
或通过inject([SearchService],(SearchService:MockSearchService)=>{…})
不幸的是,这没有帮助。使用useClass
会得到相同的结果。这是我的测试:。它仍然会导致TypeError:this.getSpy不是函数
。我不知道这是从哪里来的。我尝试将服务中的get()
方法重命名为getById()
,但错误保持不变。此错误R3InjectorError(DynamicTestModule)[SearchService->HttpClient->HttpClient]:
可能可以通过使用HttpTestingModule
解决,但我很难理解为什么需要扩展SpyObject
或SpyObject
。
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SearchComponent } from './search.component';
import { MockActivatedRoute } from '../shared/search/mocks/routes';
import { SearchService } from '../shared';
import { ActivatedRoute } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs';
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('SearchComponent', () => {
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;
let mockActivatedRoute: MockActivatedRoute;
let mockSearchService: SearchService;
beforeEach(async(() => {
mockActivatedRoute = new MockActivatedRoute({term: 'nikola'});
TestBed.configureTestingModule({
declarations: [SearchComponent],
providers: [
{provide: ActivatedRoute, useValue: mockActivatedRoute}
],
imports: [FormsModule, RouterTestingModule, HttpClientTestingModule]
}).compileComponents();
}));
beforeEach(() => {
// mock response
mockSearchService = TestBed.inject(SearchService);
mockSearchService.search = jasmine.createSpy().and.returnValue(of([]));
// initialize component
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should search when a term is set and search() is called', () => {
component = fixture.debugElement.componentInstance;
component.query = 'J';
component.search();
expect(mockSearchService.search).toHaveBeenCalledWith('J');
});
it('should search automatically when a term is on the URL', () => {
fixture.detectChanges();
expect(mockSearchService.search).toHaveBeenCalledWith('nikola');
});
});
import { EditComponent } from './edit.component';
import { TestBed } from '@angular/core/testing';
import { Address, Person, SearchService } from '../shared';
import { MockRouter, MockActivatedRoute } from '../shared/search/mocks/routes';
import { ActivatedRoute, Router } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { of } from 'rxjs';
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('EditComponent', () => {
let mockSearchService: SearchService;
let mockActivatedRoute: MockActivatedRoute;
let mockRouter: MockRouter;
beforeEach(() => {
mockActivatedRoute = new MockActivatedRoute({id: 1});
mockRouter = new MockRouter();
TestBed.configureTestingModule({
declarations: [EditComponent],
providers: [
{provide: ActivatedRoute, useValue: mockActivatedRoute},
{provide: Router, useValue: mockRouter}
],
imports: [FormsModule, HttpClientTestingModule]
}).compileComponents();
mockSearchService = TestBed.inject(SearchService);
});
it('should fetch a single record', () => {
const fixture = TestBed.createComponent(EditComponent);
const person = new Person({id: 1, name: 'Gary Harris'});
person.address = new Address({city: 'Denver'});
// mock response
spyOn(mockSearchService, 'get').and.returnValue(of(person));
// initialize component
fixture.detectChanges();
// verify service was called
expect(mockSearchService.get).toHaveBeenCalledWith(1);
// verify data was set on component when initialized
const editComponent = fixture.debugElement.componentInstance;
expect(editComponent.editAddress.city).toBe('Denver');
// verify HTML renders as expected
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h3').innerHTML).toBe('Gary Harris');
});
});
"angularCompilerOptions": {
"enableIvy": false
}