最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mock an ifelse statement with jasmine - unit testing with AngularTypescript - Stack Overflow

programmeradmin1浏览0评论

I have added a minor amendment within an existing function. Our quality checks have identified this as new code therefore I need to cover the new 4 lines with a unit test - within mind there was never a unit test there to begin with and the function I added to is pretty large!

I've tried a number of ways to try and mock services, variables, spying etc... but always get errors. I'm new to jasmine so struggling. All I need to do is get any sort of check to cover the lines truthy/falsy.

ponent.ts file

hasDescription() {
        return this.isBrilliant() || this.isCool();
    }

isBrilliant() {
        return this.service.Type.description == 'Brilliant';
    }

isCool() {
        return this.service.Type.description == 'Cool';
    }



onExportToExcel(): void {
        var r = [];
        // added the below if/else - any check on this will be suffice.
        if (this.hasDescription()) {
            if (!this.isBrilliant()) {
                r.push('This is Cool');
            } else {
                r.push('This is Brilliant');
            }
            if (!this.isBrilliant()) {
            r.push('More Cool content');
            }
        }
}

I tried to set isBrilliant() with a mock value of true, and expect that the value to be truthy expect(ponent.isBrilliant()).toBeTruthy();

I tried to set this in the spec file by:

const isBrilliant = true;
ponent.isBrilliant = isBrilliant;

however the error I got here was that Type 'true' is not assignable to type '() => boolean'.

If any experienced jasmine dev can show me a quick way to get some coverage for this simple statement that would be appreciated. Thanks


UPDATE:

I can get isBrilliant() to be true or false. I now need to check the array r to see if the correct string has been .push into it? any ideas?

I have added a minor amendment within an existing function. Our quality checks have identified this as new code therefore I need to cover the new 4 lines with a unit test - within mind there was never a unit test there to begin with and the function I added to is pretty large!

I've tried a number of ways to try and mock services, variables, spying etc... but always get errors. I'm new to jasmine so struggling. All I need to do is get any sort of check to cover the lines truthy/falsy.

ponent.ts file

hasDescription() {
        return this.isBrilliant() || this.isCool();
    }

isBrilliant() {
        return this.service.Type.description == 'Brilliant';
    }

isCool() {
        return this.service.Type.description == 'Cool';
    }



onExportToExcel(): void {
        var r = [];
        // added the below if/else - any check on this will be suffice.
        if (this.hasDescription()) {
            if (!this.isBrilliant()) {
                r.push('This is Cool');
            } else {
                r.push('This is Brilliant');
            }
            if (!this.isBrilliant()) {
            r.push('More Cool content');
            }
        }
}

I tried to set isBrilliant() with a mock value of true, and expect that the value to be truthy expect(ponent.isBrilliant()).toBeTruthy();

I tried to set this in the spec file by:

const isBrilliant = true;
ponent.isBrilliant = isBrilliant;

however the error I got here was that Type 'true' is not assignable to type '() => boolean'.

If any experienced jasmine dev can show me a quick way to get some coverage for this simple statement that would be appreciated. Thanks


UPDATE:

I can get isBrilliant() to be true or false. I now need to check the array r to see if the correct string has been .push into it? any ideas?

Share Improve this question edited Nov 3, 2020 at 15:23 Shashank Vivek 17.5k9 gold badges69 silver badges110 bronze badges asked Nov 3, 2020 at 11:45 Tom RudgeTom Rudge 3,2729 gold badges55 silver badges104 bronze badges 6
  • 1 Try const isBrilliant = () => true – ShamPooSham Commented Nov 3, 2020 at 11:51
  • If that doesn't work, I'd probably mock service.Type.description to be 'Brilliant' – ShamPooSham Commented Nov 3, 2020 at 11:52
  • thanks @ShamPooSham that helped me to get past the error assigning. Can you advise on the "update"? – Tom Rudge Commented Nov 3, 2020 at 13:24
  • 1 It will be difficult/if not impossible to get a handle on r in Jasmine since it is local to onExportToExcel. When calling onExportToExcel, what does it do? Does it change the view? If so, test that. Call it explicitly, and ensure it does its end product. – AliF50 Commented Nov 3, 2020 at 13:41
  • 1 onExportToExcel doesn't seem to do anything as it is right now. Nothing is exported, no state is mutated. As AliF50 said, you should think about what you want to do with it. – ShamPooSham Commented Nov 3, 2020 at 14:16
 |  Show 1 more ment

1 Answer 1

Reset to default 1

There are few changes that I would suggest as a part of best practices.

  1. Dont mock ponent method, because you need to test them. In your case, you should set value of this.service.Type.description and accordingly it should return true or false. That would be a correct approach.

If this.service is a service which has been injected on the construtor, you can mock the service. Refer this article to understand mocking here

  1. Since you are testing several conditions using if else , you need to write few it blocks to have a good test coverage.

  2. To test var r , you should declare it as public variable on ponent level rather than inside the function. Also prefer let over var.

Here is a sample code which you can write to set the value in isBrilliant()

it('should push Brilliant when the Description is so,()=>{
   ponent.service.Type.description = 'Brilliant';
   ponent.onExportToExcel();
   expect(ponent.r.length).toBe(1);
   expect(ponent.r[0]).toBe('This is Brilliant');
})

it('should push other cool content when the Description is not Brilliant,()=>{
   ponent.service.Type.description = 'something else';
   ponent.onExportToExcel();
   expect(ponent.r.length).toBe(2);
   // check other values in expect block accordingly
})

// you should also check that "ponent.r" has zero length when hasDescription() returns false

I hope above code snippet will give you a good start

发布评论

评论列表(0)

  1. 暂无评论