茉莉花测试服务离子2 app

时间:2017-05-02 08:46:57

标签: angular ionic-framework jasmine ionic2

我是茉莉花的新手,我试图用Jasmine测试我的离子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();
});

问题:我的新服务没有参数,也没有用。

你知道我错过了什么吗? 感谢

1 个答案:

答案 0 :(得分:1)

您需要使用TestBed创建服务,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();
    }));
 });