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?
-
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 toonExportToExcel
. When callingonExportToExcel
, 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
1 Answer
Reset to default 1There are few changes that I would suggest as a part of best practices.
- 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 returntrue
orfalse
. 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
Since you are testing several conditions using
if
else
, you need to write fewit
blocks to have a good test coverage.To test
var r
, you should declare it aspublic
variable on ponent level rather than inside thefunction
. Also preferlet
overvar
.
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