【问题标题】:Test angular snackbar测试角小吃吧
【发布时间】:2020-09-07 19:00:15
【问题描述】:

我正在制作一个简单的snackbar,代码如下,

app.component.ts:

  ngOnInit(){
    this.dataService.valueChanges.pipe(
        filter((data) =>data=== true),
        switchMap(() => {
          const snackBarRef = this.matSnackBar.open(
            'A new value updated',
            'OK',
            {
              duration: 3000
            }
          );

          return snackBarRef.onAction();
        })
      )
      .subscribe(() => {
        this.window.location.reload();
      });
  }

app.component.spec.ts(包括服务的模拟数据)

describe('AppComponent', () => { 
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;
  let matSnackBarSpy: jasmine.SpyObj<MatSnackBar>;

  let a = "";
  let b = "";
  let c = "";

  const mockDataService = {
    valueChanges: of(true)
  };

  beforeEach(async(() => {
    TestBed.configureTestingModule({

    a = "Test";
    b = "X";
    c = "suc";
    matSnackBarSpy = TestBed.get<MatSnackBar>(MatSnackBar);

 })
}))

  describe('#ngOnInit()', () => {

    it('should call MatSnackBar.open()', async(done: DoneFn) => {
      const error = new HttpErrorResponse({ error: 'Some error' });

      component.ngOnInit();

      expect(mockDataService.valueChanges).toBeTruthy();
      expect(matSnackBarSpy.open(a,b,c)).toBeTruthy();

      done();
    });
  });

})

data.service.ts

import { Observable } from 'rxjs';

export class DataService {
  valueChanges: Observable<boolean>;
}

说明:

  • 我有一项服务,其属性为valueChanges,类型为Observable&lt;boolean&gt;

  • component.ts 中,我得到了如上所述的值变化,我收到的最终结果是布尔值true,并且小吃店也打开了,一切正常。

  • 现在我正在执行上述测试用例,例如compoenent.spec.ts

      expect(mockDataService.valueChanges).toBeTruthy();
      expect(matSnackBarSpy.open(a,b,c)).toBeTruthy();
    

这会导致成功,但我永远会在 chrome 中收到以下输出。

要求:需要涵盖当前显示警告/指示上图中未涵盖的所有测试..

以上测试用例正在运行,但当我们打开组件的index.html 时,测试覆盖率仍显示未覆盖函数未覆盖语句警告。

【问题讨论】:

  • 我没有完全关注。问题,什么工具告诉你上面粉红色的内容不在测试范围内?此外,您不会想要涵盖 matSnackbar 的 open 方法,因为它不是您编写的。开发该库的人对此进行了测试。你想用你的测试来完成什么?如果它只是 100% 的代码覆盖率,我认为这不会像你想象的那样。您可能需要重新考虑如何进行测试。看看这些martinfowler.com/bliki/TestCoverage.htmlmartinfowler.com/testing

标签: angular typescript unit-testing jasmine karma-jasmine


【解决方案1】:

所以,你基本上应该测试matSnackBar 方法是否被正确调用。 matSnackBar 的测试行为不是单元测试。

试试

class MatSnackBarStub{
  open(){
    return {
      onAction: () => of({})
    }
  }

}

component.spec 文件中

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [SomeComponent],
      providers ; [ { provide: MatSnackBar , useClass: MatSnackBarStub }]
    }).compileComponents();
  }));

  it('should create', () => {
    spyOn(component.matSnackBar,"open").and.callThrough();
    component.ngOnInit();
    expect(component.matSnackBar.open).toHaveBeenCalled();
    // you can also use ".toHaveBeenCalledWith" with necessary params
  });

我建议您查看与使用 jasmine 和 karma 进行单元测试相关的 this collection of articles。有一篇关于如何使用存根和间谍的文章。我希望这会有所帮助

【讨论】:

  • 你能帮我吗stackoverflow.com/q/62130441/13270726 ..我被严重卡住了..我可以看到你的许多与角度测试相关的帖子,我相信你将能够为我在那个问题..请帮助我..
  • @Undefined:当然。给我一个小时。我会写一个答案:)
  • 感谢您的回复.. 希望您能提供良好的工作解决方案..
  • 嗨 Vivek,很抱歉再次回来,请您帮我完成这个stackoverflow.com/q/62153001/13270726 的测试。同时我已经让团队负责人接受您提供的实施服务的解决方案方式..
猜你喜欢
  • 2018-09-13
  • 1970-01-01
  • 2018-11-16
  • 2018-07-03
  • 2020-02-20
  • 2020-10-18
  • 2015-02-13
  • 2018-09-12
  • 2019-07-24
相关资源
最近更新 更多