2017-07-10 1 views

答えて

0

ここでは、テストサービスを開始するための最小限の例を示します。ほとんどの状況では、あなたはテストベッドを使用する必要はありませんので、私は、この最小単位の試験例の外にいることを左:

Abcのサービス:

import { Injectable } from '@angular/core'; 

@Injectable() 
export class AbcService { 

    constructor(private otherService) { 
    } 

    add(x: number, y: number) { 
    return x + y; 
    } 

    submit(x) { 
    this.otherService.submit(x); 
    } 

} 

Abcのサービスspecファイル:

import { AbcService } from './abc'; 


describe('AbcService',() => { 

    it('should add 1 + 2',() => { 
    // Arrange 
    const sut = new AbcService(null); 

    // Act 
    const result = sut.add(1, 2); 

    // Assert 
    expect(result).toEqual(3); 
    }); 

    it('should dispatch SET_INTERACTION when setAnswer is run',() => { 

    // Arrange 
    const mockOtherService = jasmine.createSpyObj('mockOtherService', ['submit']); 
    const sut = new AbcService(mockOtherService); 

    // Act 
    sut.submit(10); 

    // Assert 
    expect(mockOtherService.submit).toHaveBeenCalledWith(10); 
    }); 
}); 
+0

すばらしい、ありがとう –

0
describe('Test Service',() => { 
    let yourCurrentService: YouCurrentService; 
    let yourServiceDependencyService : YourServiceDependencyService; 

    beforeEach(() => { 

     let injector = ReflectiveInjector.resolveAndCreate([ 
     YouCurrentService, 
     YourServiceDependencyService 
     ]); 
     yourCurrentService = injector.get(YourServiceDependencyService); 
     yourCurrentService = injector.get(YouCurrentService); 
    }); 

    it('test',() => { 

    }); 
}); 
関連する問題