因果报应亮点订阅

时间:2018-09-03 05:57:31

标签: angular jasmine karma-jasmine

这是我的组件代码。我正在尝试实现完整的代码覆盖范围,但第四行已突出显示res => { this.days = res

这是为Details服务添加的代码

constructor(public detailsService:DashboardService ) {
   this.csvContent = '';
}

public getNonDealingDates() {
  try {
    this.detailsService.nonDealingdates().subscribe(
      res => { this.days = res },
      error => { throw error; }
    );
  } catch(excep){ }
}

这是我的单元测试用例。

it('should get non dealing dates ',() => {
  component.getNonDealingDates;
  expect(component.getNonDealingDates()).toBeUndefined();
})

我需要包括要订阅的模拟吗?帮助我如何为上述代码做模拟

1 个答案:

答案 0 :(得分:0)

我认为您的组件看起来像这样。

import { Component } from '@angular/core';
import { DetailsService } from 'path/to/your/service';

@Component({
  selector: 'app-your-component-name',
  styleUrls: ['./app-your-component-name.css'],
  templateUrl: './app-your-component-name.html'
})
export class YourComponent { 

public days: any; // replace any with the correct data type.

 constructor(private detailsService: DetailsService) {}

  public getNonDealingDates() {
    try {
      this.detailsService.nonDealingdates().subscribe(
        res => {
        this.days = res
        }, error => {
          throw error;
        });
    } catch (excep) {

    }
  }
}

对于这样的类,我们可以编写这样的单元测试。

import { Input, NO_ERRORS_SCHEMA } from '@angular/core';

import { Subject } from 'rxjs/internal/Subject';

import { DetailsService } from 'path/to/your/service';
import { YourComponent } from 'path/to/your/component';


describe('YourComponent tests', () => {
    let fixture: ComponentFixture<YourComponent>;
    let component: YourComponent;

    let detailsService: DetailsService;
    let spyDetailsService: jasmine.SpyObj<DetailsService>;

    let subject;

    const response: any = {};

    beforeEach(async(() => {
      subject = new Subject();

      spyDetailsService = jasmine.createSpyObj('DetailsService', ['nonDealingdates']);
      spyDetailsService.nonDealingdates.and.returnValue(subject.asObservable());

      TestBed.configureTestingModule({
        declarations: [YourComponent],
        providers: [
          { provide: DetailsService, useValue: spyDetailsService }
        ],
        schemas: [NO_ERRORS_SCHEMA]
      }).compileComponents().then(() => {
        fixture = TestBed.createComponent(YourComponent);
        component = fixture.componentInstance;

        detailsService = TestBed.get(DetailsService);
      });
    }));

    afterEach(() => {
      subject = null;
    });

    describe('#getNonDealingDates tests', () => {
      it('Should set the days to the value returned by the detailsService.nonDealingdates()', () => {
        component.getNonDealingDates();
        subject.next(response);

        expect(component.days).toEqual(response);
      });
    });
});

在这里,我以DetailsService的形式提供了模拟服务。

spyDetailsService = jasmine.createSpyObj('DetailsService', ['nonDealingdates']);

我已经使用jasmine.createSpyObject方法创建了一个spyObject。

然后看一下providers数组。

providers: [
          { provide: DetailsService, useValue: spyDetailsService }
        ],

我提供了模拟对象,而不是提供实际的服务。

当您的详细信息服务的nonDealingdates方法返回可观察值时,我正在将模拟的服务方法中的主题调整为可观察值。

spyDetailsService.nonDealingdates.and.returnValue(subject.asObservable());

在测试getNonDealingDates方法时,您必须调用 subject.next(),以服务响应作为参数。