@Injectable()
export class NavService {
constructor (
public pagesToLoad
) {
this.pagesToLoad = [1,2,3,4,5]
}
public lookAround(currentPage){
let tuple = (this.pagesToLoad.indexOf(currentPage)-1,this.pagesToLoad.indexOf(currentPage)+1)
return tuple
}
import {Modal, Alert, NavParams, ViewController, Events} from 'ionic-angular';
import {provide,ReflectiveInjector,Component,PLATFORM_PIPES} from '@angular/core'
import { HTTP_PROVIDERS, XHRBackend, Http } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { getDOM } from '@angular/platform-browser/src/dom/dom_adapter';
import {
TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
} from '@angular/platform-browser-dynamic/testing';
import {
beforeEach,
beforeEachProviders,
describe,
expect,
it,
xit,
xdescribe,
ComponentFixture,
inject,
async,
setBaseTestProviders,
TestComponentBuilder
} from '@angular/core/testing'
import {NavService} from './nav-service';
let tcb;
let navService;
beforeEachProviders(() => [
provide(navService, {useClass: NavService})
]);
beforeEach(inject(
[
TestComponentBuilder,
NavService
],
(
_tcb,
_navService
) => {
tcb = _tcb
navService = _navService
}));
describe('NavService', () => {
it ('should have instantiated nav service', () => {
expect(this.navService instanceof NavService).toBeTruthy()
})
})
TypeError: Cannot read property 'lookAround' of undefined
应该很直接,但我的测试失败了。怎么了?
答案 0 :(得分:0)
当然,直接测试服务时我不必提供依赖项。所以这就像这样:
import {NavService} from './nav-service'
describe('NavService', () => {
it ('should have instantiated nav service', () => {
let navService = new NavService();
expect(navService.lookAround(3)).toEqual((2,4))
})
})
Chrome 51.0.2704(Mac OS X 10.11.3):执行1/4(跳过3次)成功(0.013秒/ 0.005秒)