Javascript 如何对TweenMax进行单元测试?潜在的嘲笑?
我正在尝试对我的函数进行单元测试,该函数使用了GSAP中的TweenMax,但我不确定如何最好地进行测试Javascript 如何对TweenMax进行单元测试?潜在的嘲笑?,javascript,angular,typescript,unit-testing,testing,Javascript,Angular,Typescript,Unit Testing,Testing,我正在尝试对我的函数进行单元测试,该函数使用了GSAP中的TweenMax,但我不确定如何最好地进行测试 @ViewChild('dropdown') dropdown: ElementRef; expanded = false; toggleDropdown() { const dropdown = this.dropdown.nativeElement; TweenMax.to(dropdown, 0.5, { css: { height: t
@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,但我的尝试收到了错误:
应为间谍,但获得了对象({to:spy on unknown.to})
抱歉,我删除了以前的答案,因为它太长了 在您提供的stackblitz之后,我自己也创建了一个: 正如你所看到的,它就像一个符咒
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();
});
});
fdescribe('TweenComponent',()=>{
let夹具:组件夹具,组件:TweenComponent;
在每个之前(()=>{
TestBed.configureTestingModule({
声明:[两个组成部分]
}).compileComponents();
fixture=TestBed.createComponent(TweenComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该调用Tween.to',()=>{
spyOn(TweenMax,to');
toggleDropdown();
期望(TweenMax.to).tohavebeincalled();
});
});
如果您可以重现该问题,请随意编辑此闪电战,否则问题就在您的代码中!什么?!我真的只是复制了的风格,而且成功了?!我们以前试图创建spyObj时一定出了什么问题?你知道我可以用什么方法检查实际的CSS值是否发生了变化吗?我所做的一切都是失败的。在本文中,我将编写:expect(component.myElement.nativeElement.style.height).toBe('50px')你需要使用spyOn(TweenMax,'to')。和.callThrough()代码>,否则不调用该函数(替换为spy)。另外,考虑使用<代码>夹具。代码>如果测试仍然失败!但是,正如我们在其他主题中讨论的那样,您应该期望您的函数是使用给定参数调用的,而不是检查函数是否工作:通过这样做,您抽象了依赖项的行为,使您的单元测试成为真正的单元测试(而不是依赖于第二个单元的单元测试)
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();
});
});