我正在尝试对使用GSAP中的TweenMax的函数进行单元测试,但是我不确定如何最好地实现它。

@ViewChild('dropdown') dropdown: ElementRef;
expanded = false;

toggleDropdown() {
    const dropdown = this.dropdown.nativeElement;

    TweenMax.to(dropdown, 0.5, {
      css: {
        height: this.expanded ? '34px' : '376px'
      },
      ease: Power2.easeInOut,
    });

    this.expanded = !this.expanded;
  }


我当时正在考虑嘲笑TweenMax,但我尝试的尝试收到了以下错误:


  本来是间谍,但得到了Object({to:spy on unknown.to})。


beforeEach(async(() => {
    mockTween = createSpyObj(['to']);

    TestBed.configureTestingModule({
      imports: [...],
      declarations: [...],
      providers: [... ,
        {provide: TweenMax, useValue: mockTween}
      ]
    });

    ...
  }));

it('should set dropdown height to 376px if not currently expanded', () => {
    component.expanded = false;

    component.toggleDropdown();

    expect(mockTween).toHaveBeenCalled();
  });

最佳答案

抱歉,我删除了以前的答案,因为答案太长了。

在您提供的堆栈闪电之后,我还创建了一个我自己:

https://stackblitz.com/edit/angular-6-jasmine-tween-j5wsz9?file=app/tween/tween.component.spec.ts

如您所见,它就像一个魅力。

fdescribe('TweenComponent', () => {
  let fixture: ComponentFixture<TweenComponent>, component: TweenComponent;
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [TweenComponent]
    }).compileComponents();

    fixture = TestBed.createComponent(TweenComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should call Tween.to', () => {
    spyOn(TweenMax, 'to');
    component.toggleDropdown();
    expect(TweenMax.to).toHaveBeenCalled();
  });
});

关于javascript - 如何对TweenMax进行单元测试?潜在的 mock ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52814224/

10-11 10:41