我是新来的茉莉花,我正在尝试用茉莉花测试我的离子2服务。
我的服务:
import {Injectable} from '@angular/core';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
import {User} from '../Entity/user';
import {SecureStorageServices} from '../Providers/secureStorageServices';
@Injectable()
export class myService {
constructor(private secureStorageServices: SecureStorageServices, private user: User) {
}
//Some other methods which I want to unit test.
我必须在构造函数中提供服务。
我试图做:
describe('Service: my Service', () => {
it('should do xxx', () => {
let service = new myService();
expect(service).toBeDefined();
});
问题:我的新服务没有参数,也无法正常工作。
你知道我缺少什么吗?谢谢
您需要使用将容纳相关导入的测试台创建服务。因为您需要以某种方式将参数注入构造函数。
所以应该这样做:
describe('Service: my Service', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [],
imports: [
],
providers: [
SecureStorageServices, // all related providers here
User,
myService
]
})
}));
it('should do xxx', inject([myService], (service) => {
expect(service).toBeDefined();
}));
});