14

I am unable to test getter and setter functions in Angular 5 using Karma. Any ideas as to where I'm going wrong?

The value is set and then the test waits a short duration for the setter function to resolve before calling the getter function. Here is the code:

service.ts

set header(h: string) {
    this.zone.run(() => {
        this._header = h;
    });
}

get header(): string {
    return this._header;
}

test.ts

before(() => {
    service = new Service()
});

it('updates header', (done) => {
    const header = 'test header';
    service.header = header;

    // Give header time to update
    return Promise.delay(200)
    .then(() => {
         expect(service.header).to.equal(header);
    })
});

I would expect the assertion to resolve successfully however I get an error

AssertionError: expected undefined to equal 'test header'

Andy Richardson
  • 181
  • 1
  • 1
  • 7
  • 1
    Did you find any solution on how to properly unit test setters/getters in angular? – Amit Chigadani May 28 '18 at 10:11
  • Maybe that way ? ```const spy = spyOnProperty(service, 'header', 'get').andReturn('test header'); expect(service.header).toBe('test header'); expect(spy).toHaveBeenCalled();``` – Mateusz Nov 30 '18 at 13:56
  • Try adding `this.cd.detectChanges()` after assigining `service.header = header`. Where `cd` is an instance of `ChangeDetectionRef` – Christian Vincenzo Traina Dec 18 '18 at 14:18

1 Answers1

3

Is there a reason that you wrap your set content into a zone ?

For 99% of all async activities i love the "fakeAsync" and the "tick" method of Angular.

it('should update header', fakeAsync( ()=>{
  const header = 'test header';
  service.header = header;
  tick() // <= Will wait for for all microtransactions to finish
  expect(service.header).to.equal(header);
}))

There are quite a few good articles about Macro- and Microtransactions in JavaScript. The short version is, that Promises and Subscriptions of an observable-emit are microtransactions.

"setTimeout" is an example for a macrotransaction.

The best about "tick()" in a fakeAsync-UnitTest is, that you dont have to make sugestions how long the code will need to wait. "tick()" just drains the microtransaction-queue so all waiting micro transactions will be resolved, before the test proceeds.

You can use them also when waiting for the changeDetection of Angular-components. :-)

I hope that helps a bit.

warm regards

priya_21
  • 159
  • 1
  • 2
  • 15
JanRecker
  • 1,787
  • 13
  • 17