Skip to content

Instantly share code, notes, and snippets.

@ssergdev
Created April 12, 2021 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssergdev/6e88de0f2d0d60306269211707eaff26 to your computer and use it in GitHub Desktop.
Save ssergdev/6e88de0f2d0d60306269211707eaff26 to your computer and use it in GitHub Desktop.
@Injectable({
providedIn: 'root',
})
export class SomeService {
doSomething() {
console.log('test');
}
}
@Component({
selector: 'some-component',
template: '',
})
export class SomeComponent implements OnInit {
constructor(private service: SomeService) {}
ngOnInit(): void {
this.service.doSomething();
}
}
describe('SomeComponent', () => {
let service: SomeService;
let component: SomeComponent;
let fixture: MockedComponentFixture<SomeComponent>;
beforeEach(() => {
// In this example i'm using 'keep' because in real situation i can not mock dependency,
// but want to spy on it methods
return MockBuilder(SomeComponent).keep(SomeService);
});
beforeEach(() => {
service = TestBed.inject(SomeService);
spyOn(service, 'doSomething');
fixture = MockRender(SomeComponent);
component = fixture.point.componentInstance;
});
it('should do something initially', () => {
expect(service.doSomething).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment