Unit testing 带存根的angular 2中jasmine测试用例依赖性
在这里,我是jasmine+karma的angular 2测试用例的新手,我遵循这一点,我尝试编写测试用例,但无法正确编写,并且由于测试用例在运行时将进入服务器而导致路由错误,尽管它不应该这样做。 有一个问题与类似,但没有帮助我解决问题。请引导我 这是我的组件Unit testing 带存根的angular 2中jasmine测试用例依赖性,unit-testing,angular,karma-jasmine,angular2-routing,stubs,Unit Testing,Angular,Karma Jasmine,Angular2 Routing,Stubs,在这里,我是jasmine+karma的angular 2测试用例的新手,我遵循这一点,我尝试编写测试用例,但无法正确编写,并且由于测试用例在运行时将进入服务器而导致路由错误,尽管它不应该这样做。 有一个问题与类似,但没有帮助我解决问题。请引导我 这是我的组件 import { Component, OnInit, ViewChild } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Checklist } from './checklist';
import { Job } from '../+job/job';
import { OriginalService } from './original.service';
import { UserService } from '../core/user/user.service';
import { ModalDirective } from 'ng2-bootstrap/ng2-bootstrap';
@Component({
selector: 'app-selector',
templateUrl: './original.component.html',
providers: [OriginalService]
})
export class OriginalComponent implements OnInit {
items = []
job: Job;
checklist: Checklist;
user: any;
shopId: any;
jobId: any;
constructor ( private orgService: OriginalService, private route: ActivatedRoute, private router: Router, userService: UserService) {
this.user = userService.user();
}
ngOnInit() {
this.route.params
.map(params => params['job_id'])
.subscribe(
job_id => this.jobId = job_id
);
this.getChecklist();
this.getJob();
}
getChecklist() {
this.orgService.getChecklists({
jobId: this.jobId
})
.then(checklist=> {
this.gotJobdata = true;
this.checklist = checklist.response})
.catch(error => this.error = error);
}
getJob(){
this.orgService.getJob({
jobId: this.jobId
})
.then(job => this.job = job.response)
.catch(error => this.error = error);
}
}
这是我的服务
import { Injectable, ViewChildren } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { environment } from '../../environments/environment'
import 'rxjs/add/operator/toPromise';
import { Checklist } from './checklist';
import { Job } from '../+job/job';
import { UserService } from '../core/user/user.service';
@Injectable()
export class OriginalService {
shopId: any;
constructor(private http: Http, private userService: UserService ) {
this.shopId = userService.shopId();
}
getChecklists(svc): Promise<Checklist> {
return this.http.get(environment.apiUrl + this.shopId + '/jobs/' + svc.jobId + '/checklists_path/' + 'check_item.json')
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
getJob(svc): Promise<Job> {
return this.http.get(return environment.apiUrl + this.shopId + '/jobs/' + svc.jobId + '.json')
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
}
从'@angular/core'导入{Injectable,ViewChildren};
从'@angular/Http'导入{Http,Response,Headers,RequestOptions};
从“../../environments/environment”导入{environment}
导入“rxjs/add/operator/toPromise”;
从“./Checklist”导入{Checklist};
从“../+Job/Job”导入{Job};
从“../core/user/user.service”导入{UserService};
@可注射()
导出类原始服务{
肖皮德:有;
构造函数(私有http:http,私有userService:userService){
this.shopId=userService.shopId();
}
GetChecklist(svc):承诺{
返回此.http.get(environment.apirl+this.shopId++'/jobs/'+svc.jobId++'/checklists\u path/'+'check\u item.json'))
.toPromise()
.then(response=>response.json())
.接住(这个.把手错误);
}
getJob(svc):承诺{
返回this.http.get(返回environment.apirl+this.shopId+'/jobs/'+svc.jobId+'.json')
.toPromise()
.then(response=>response.json())
.接住(这个.把手错误);
}
}
以下是我尝试过的规格:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SharedModule } from '../shared/shared.module';
import { ModalModule } from 'ng2-bootstrap/ng2-bootstrap';
import { HttpModule } from '@angular/http';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { ComponentLoaderFactory } from 'ng2-bootstrap/component-loader';
import { Subject } from 'rxjs/Subject';
import { UserService } from '../core/user/user.service';
import { OriginalService } from './original.service';
import { OriginalComponent } from './original.component';
describe('OriginalComponent', () => {
let component: OriginalComponent;
let fixture: ComponentFixture<OriginalComponent>;
let params: Subject<Params>;
let userService, orgService;
let userServiceStub = {
isLoggedIn: true,
user: { name: 'Test User'}
};
beforeEach(async(() => {
params = new Subject<Params>();
TestBed.configureTestingModule({
imports: [ ModalModule.forRoot(), SharedModule, HttpModule ],
declarations: [ OriginalComponent ],
providers: [ OriginalService, UserService, ComponentLoaderFactory,
{ provide: Router, useValue: userServiceStub }, {provide: ActivatedRoute, useValue: { params: params }} ]
})
.compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(ChecklistComponent);
component = fixture.componentInstance;
fixture.detectChanges();
it('should create', () => {
expect(component).toBeTruthy();
});
});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从“../shared/shared.module”导入{SharedModule};
从“ng2引导/ng2引导”导入{ModalModule};
从'@angular/http'导入{HttpModule};
从'@angular/Router'导入{Router,ActivatedRoute,Params};
从“ng2引导/组件加载程序”导入{ComponentLoaderFactory};
从'rxjs/Subject'导入{Subject};
从“../core/user/user.service”导入{UserService};
从“/original.service”导入{OriginalService};
从“./origin.component”导入{OriginalComponent};
描述('OriginalComponent',()=>{
let组件:原始组件;
let夹具:组件夹具;
让参数:主体;
让userService,orgService;
让userServiceStub={
伊斯洛格丁:是的,
用户:{name:'测试用户'}
};
beforeach(异步(()=>{
params=新主题();
TestBed.configureTestingModule({
导入:[ModalModule.forRoot(),SharedModule,HttpModule],
声明:[原始组件],
提供者:[OriginalService,UserService,ComponentLoaderFactory,
{provide:Router,useValue:userServiceStub},{provide:ActivatedRoute,useValue:{params:params}]
})
.compileComponents();
})
);
在每个之前(()=>{
fixture=TestBed.createComponent(ChecklistComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});
请纠正我如何在我做错的测试用例或存根中正确使用路由
您得到的错误是什么?当我运行测试用例时,它从Id未定义的服务调用API,并从服务器返回路由错误。