我是 Jasmine 的新手,我正在尝试使用 Jasmine 测试我的 ionic 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();
});
问题:我的新服务没有参数,不工作。
你知道我缺少什么吗?谢谢
您需要使用将保存相关导入的 TestBed 创建您的服务。因为您需要以某种方式将参数注入到构造函数中。
所以它应该是这样的:
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();
}));
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句