-
-
Save ssergdev/6e88de0f2d0d60306269211707eaff26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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