角度2测试模拟服务

时间:2017-03-02 09:05:38

标签: unit-testing angular

我正试图在我的角度2测试中模拟一个服务,但它只是继续使用真实的一个,并给我一些与未找到该服务的依赖关系相关的错误

这是我的组件文件

import {Component, Input} from '@angular/core';
import {ClientService} from '../client.service';

@Component({
    selector: 'registry',
    templateUrl: './registry.component.html',
    styleUrls: [
        './registry.component.scss'.toString()
   ]
})

export class RegistryComponent {    

    constructor(private clientService: ClientService) {
    }

    loadResources() {
        return this.clientService.getBusinessNetwork()
    }
}

这是我的测试文件

import {NO_ERRORS_SCHEMA} from '@angular/core';
import {inject, async, TestBed, ComponentFixture} from '@angular/core/testing';
import {Component} from '@angular/core';

import * as sinon from 'sinon';

import {RegistryComponent} from './registry.component';
import {ClientService} from '../client.service';

describe(`Registry`, () => {
   let comp: RegistryComponent;
   let fixture: ComponentFixture<RegistryComponent>;

   beforeEach(() => {
       let MockClientService = {
           getBusinessNetwork : sinone.stub()
       };

       TestBed.configureTestingModule({
           declarations: [RegistryComponent],
           schemas: [NO_ERRORS_SCHEMA],
           providers: [
               {provide: ClientService, useValue: MockClientService},
           ]
       })
         .compileComponents(); // compile template and css
    }));

    beforeEach(() => {
      fixture = TestBed.createComponent(RegistryComponent);
      comp = fixture.componentInstance;

      fixture.detectChanges(); // trigger initial data binding
    });

    it('should have default data', () => {
        comp.should.be.true;
    });
});

有人可以帮忙吗?

0 个答案:

没有答案