I don't know why it didn't work within your case but I manage to create simple test case and it's working properly:
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');
});
});
One thing to note - do not import external modules to TestingModule
as you want to test only your component rather do mock/spy upon required dependencies.