服务上的Angular2单元测试失败TypeError:service.getValue不是函数

时间:2018-03-23 09:26:41

标签: angular unit-testing

  

TypeError:service.getValue不是函数

let data: string;

@Injectable()
export class MyService {
constructor() {
    myOtherObservableReturning.subscribe((data: mydata) => {
           data.items.forEach((item: Object)  => {
               .....
           }
        ...
        ...
    });
}
   public getValue() { return 'HelloWorld'; }
}

单元测试:

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [
            { provide: MyService, useValue: { data: Observable.of(dataStub) } }
        ]
    });
    service = TestBed.get(MyService);
});

it('should be created', () => {
    expect(service).toBeTruthy();
    console.log(JSON.stringify(service)); 
    //LOG: '{"params":{"_isScalar":true,"scheduler":null}}'
});


it('should call getValue', () => {
    //TypeError: service.getValue is not a function
    expect(service.getValue()).toBe('HelloWorld');
});

我无法弄清楚为什么单元测试无法找到函数getValue(),我已经在服务文件中导入并检查了它。

0 个答案:

没有答案