尝试使用jasmine对angular2应用程序进行单元测试,但是当我尝试注入服务时,间谍不会接受注入的调用.
测试套件:
import { TestBed, inject, tick, fakeAsync, ComponentFixture } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';
import { RoomComponent } from './room.component';
import { Room } from './room';
import { RoomService } from './room.service';
import { MockRoomService } from '../mock/mock.room.service';
import { BaseRequestOptions, Http, ConnectionBackend, Response, ResponseOptions } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
...
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
declarations: [
RoomComponent
],
providers: [
{
provide: Http, useFactory: (backend: …
Run Code Online (Sandbox Code Playgroud) 我正在编写TestBed单元测试。
有一个特定的组件,它是被测组件的子组件。测试运行时,该子组件会导致错误。该子组件与测试本身无关;这只是造成问题。
我想用一个假人替换它,或防止它被添加。
有问题的组件来自被测组件以外的其他模块。
我试图做一个存根/虚拟:
@Component({
selector : 'problematic-component-selector',
template : 'FAKE CAPTCHA',
})
export class ProblematicComponentStubComponent {
}
Run Code Online (Sandbox Code Playgroud)
这是我的beforeEach
:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
ReactiveFormsModule,
FormsModule,
RouterModule,
ModuleOfProblematicComponent,
],
declarations: [
ComponentUnderTest,
ProblematicComponentStubComponent, /* NOTE: here I tried to declare the fake one */
],
providers: [
{ provide: Router, useClass: RouterStub },
{ provide: ActivatedRoute, useClass: Stub },
],
Run Code Online (Sandbox Code Playgroud)
我尝试覆盖组件模板,以防止出现以下错误:
TestBed.overrideComponent(ProblematicComponent, {
set: {
template: 'Fake Captcha' // prevent ReCaptcha error
}
}) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 TestBed from 来测试在其 HTML 中具有 ng-select 的 Angular 组件。我首先收到错误消息,说无法绑定到“项目”,因为它不是“ng-select”的已知属性。所以我导入了 NgSelectModule 并将其添加到 TestBed 配置中的导入中。它现在返回Can't bind to 'ngModel' 因为它不是 'ng-select' 的已知属性
import { getTestBed, TestBed } from '@angular/core/testing';
import { ProductGenericGridComponent } from './product-generic-grid.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ProductGridService } from './product-generic-grid.service';
import { NgSelectModule } from '@ng-select/ng-select';
import { ProductItemComponent } from './../product-item/product-item.component';
describe('Product Generic Grid Component', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ProductGenericGridComponent],
imports: [HttpClientTestingModule, NgSelectModule],
providers: []
});
}); …
Run Code Online (Sandbox Code Playgroud) 我有一个包含iframe
. 为了防止在测试中从 iframe 加载不存在的 URL,我想模拟组件的模板。我以为我可以用TestBed.overrideComponent()
它来做,但它没有效果。当测试运行时,我可以看到原始模板存在并且 iframe 加载不存在的 url。
我试过的:
fixture = TestBed.overrideComponent(IFrameComponent, {
remove: {
templateUrl: './iframe.component.html'
},
add: {
template: '<div></div>'
}
}).createComponent(IFrameComponent);
Run Code Online (Sandbox Code Playgroud)
如何覆盖要使用的组件template
而不是templateUrl
?
为什么要在VHDL中创建测试平台/测试平台?坐在和操纵模拟器中的信号以确保VHDL代码正常运行不是一样好吗?
我尝试在我的应用程序中模拟请求,但是流程变量存在问题.我将process.env.backendUrl url存储在后端API中.然后在RestService我有:
constructor(private http: Http) {
this.rest = process.env.backendUrl + "/api/";
}
Run Code Online (Sandbox Code Playgroud)
现在不可能运行测试,因为在例如LoginComponent我有RestService依赖,我有这个错误:
zone.js:140 Uncaught Error: Error in ./LoginComponent class LoginComponent_Host - inline template:0:0 caused by: process is not defined
ReferenceError: process is not defined
at new RestService (http://localhost:9876/base/src/test.ts:21595:2790)
at DynamicTestModuleInjector.get (DynamicTestModule.ngfactory.js:170:67)
at DynamicTestModuleInjector.get (DynamicTestModule.ngfactory.js:180:93)
at DynamicTestModuleInjector.getInternal (DynamicTestModule.ngfactory.js:255:51)
at DynamicTestModuleInjector.NgModuleInjector.get (http://localhost:9876/base/src/test.ts:25036:27)
at TestBed.get (http://localhost:9876/base/src/test.ts:5589:51)
at _View_LoginComponent_Host0.createInternal (LoginComponent_Host.ngfactory.js:16:74)
at _View_LoginComponent_Host0.AppView.create (http://localhost:9876/base/src/test.ts:36192:21)
at _View_LoginComponent_Host0.DebugAppView.create (http://localhost:9876/base/src/test.ts:36404:44)
Run Code Online (Sandbox Code Playgroud)
我在enviroment.ts(由angular-cli创建的文件)中设置了proccess.env.backendUrl.
process.env.backendUrl = 'http://localhost:8080';
export const environment = {
production: false
};
Run Code Online (Sandbox Code Playgroud)
我应该将它设置在其他地方还是有任何方法可以告诉业力这个变量?
我一直在角网站上看测试教程.我很好奇是否有可通过debugElement进行测试的所有项目列表.例如
let nav = fixture.debugElement.query(By.css('h1')).nativeElement;
expect(nav.innerText).toBe(fixture.componentInstance.homeHeader);
Run Code Online (Sandbox Code Playgroud)
还有哪些其他元素,比如我正在测试的innerText值.感谢您的任何帮助.
我正在编写一个Angular 4应用程序,编写单元案例,我正面临着问题。
component.ts
ngOnInit() {
this.service.getData().subscribe(res => {
console.log(res);
this.data= JSON.parse(res._body);
this.addFormControls();
},
error => {
console.log(error);
});
}
Run Code Online (Sandbox Code Playgroud)
组件规格
describe('component', () => {
let userRolesService: LayoutService;
const modulesData = {
'moduleName':'Info'
}
class MockUserRolesService {
public getModulesData() {
return modulesData;
}
}
beforeEach(async(() => {
TestBed.configureTestingModule({
providers:[ {provide: LayoutService, useClass: MockUserRolesService } ]
})
.compileComponents();
}));
beforeEach(() => {
userRolesService = TestBed.get(LayoutService);
});
afterEach(() => {
userRolesService = null;
component = null;
});
it('should fetch module data', () => …
Run Code Online (Sandbox Code Playgroud) 我想为我的服务编写测试,我想确保参数发送正常.我可以测试它
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { JhiPaginationUtil } from '.';
@Injectable()
export class JhiResolvePagingParams implements Resolve<any> {
constructor(private paginationUtil: JhiPaginationUtil) { }
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const page = route.queryParams['page'] ? route.queryParams['page'] : '1';
const defaultSort = route.data['defaultSort'] ? route.data['defaultSort'] : 'id,asc';
const sort = route.queryParams['sort'] ? route.queryParams['sort'] : defaultSort;
return {
page: this.paginationUtil.parsePage(page),
predicate: this.paginationUtil.parsePredicate(sort),
ascending: this.paginationUtil.parseAscending(sort)
};
}
}
Run Code Online (Sandbox Code Playgroud) testbed ×9
angular ×8
unit-testing ×4
testing ×3
jasmine ×2
mocking ×2
typescript ×2
components ×1
javascript ×1
vhdl ×1