When is a Test not a Unit-test?

See Michael Feathers’ definition A test is not a unit test if: It talks to the database It communicates across the network It touches the file system It can’t run at the same time as any of your other unit tests You have to do special things to your environment (such as editing config files) … Read more

Is there an easy way to stub out time.Now() globally during test?

With implementing a custom interface you are already on the right way. I take it you use the following advise from the golang-nuts thread you’ve posted: type Clock interface { Now() time.Time After(d time.Duration) <-chan time.Time } and provide a concrete implementation type realClock struct{} func (realClock) Now() time.Time { return time.Now() } func (realClock) … Read more

How to unit test a component that depends on parameters from ActivatedRoute?

The simplest way to do this is to just use the useValue attribute and provide an Observable of the value you want to mock. RxJS < 6 import { Observable } from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; … { provide: ActivatedRoute, useValue: { params: Observable.of({id: 123}) } } RxJS >= 6 import { of } from ‘rxjs’; … Read more

Any way to test EventEmitter in Angular2?

Your test could be: it(‘should emit on click’, () => { const fixture = TestBed.createComponent(MyComponent); // spy on event emitter const component = fixture.componentInstance; spyOn(component.myEventEmitter, ’emit’); // trigger the click const nativeElement = fixture.nativeElement; const button = nativeElement.querySelector(‘button’); button.dispatchEvent(new Event(‘click’)); fixture.detectChanges(); expect(component.myEventEmitter.emit).toHaveBeenCalledWith(‘hello’); }); when your component is: @Component({ … }) class MyComponent { @Output myEventEmitter … Read more

Is duplicated code more tolerable in unit tests?

Readability is more important for tests. If a test fails, you want the problem to be obvious. The developer shouldn’t have to wade through a lot of heavily factored test code to determine exactly what failed. You don’t want your test code to become so complex that you need to write unit-test-tests. However, eliminating duplication … Read more

tech