Sak*_*ken 6 spy jasmine angular
如何监视clipboard.copy方法?为了
const clipboard = TestBed.inject(Clipboard);
spyOn(clipboard, 'copy').and.returnValue(true);
Run Code Online (Sandbox Code Playgroud)
我收到警告
Argument of type '"copy"' is not assignable to parameter of type 'keyof Clipboard'.
Run Code Online (Sandbox Code Playgroud)
这是CopyToClipboardHost
class CopyToClipboardHost {
public content = '';
public attempts = 1;
public copied = jasmine.createSpy('copied spy');
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它在您的案例中不起作用,但我设法创建简单的测试案例并且它工作正常:
import {Component} from '@angular/core';
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {Clipboard} from '@angular/cdk/clipboard';
import createSpyObj = jasmine.createSpyObj;
@Component({
selector: 'app-root',
template: ''
})
export class SampleComponent {
constructor(private clipboard: Clipboard) {
}
copySomething(): void {
this.clipboard.copy('test');
}
}
describe('SampleComponent', () => {
let fixture: ComponentFixture<SampleComponent>;
let component: SampleComponent;
const clipboardSpy = createSpyObj<Clipboard>('Clipboard', ['copy']);
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [SampleComponent],
providers: [{provide: Clipboard, useValue: clipboardSpy}]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SampleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should call clipboard copy', () => {
component.copySomething();
expect(clipboardSpy.copy).toHaveBeenCalledWith('test');
});
});
Run Code Online (Sandbox Code Playgroud)
需要注意的一件事 - 不要导入外部模块,因为TestingModule您只想测试您的组件,而不是对所需的依赖项进行模拟/监视。
| 归档时间: |
|
| 查看次数: |
3716 次 |
| 最近记录: |