I have initialised allUsers
by getting it from cache then assigned boolean value to specialUserExists
from allUsers
using allUsers.some()
(Array.prototype.some() for reference).
service.ts
@Injectable({
providedIn: 'root'
})
export class SomeService {
private allUsers: User[] = this.storageService.getItem('SOME_KEY');
private specialUserExists = this.allUsers.some(user => user.inSpecialGroup);
constructor(
private filterService: FilterService,
private storageService: StorageService,
) { }
}
In service.specs.ts
I have written test case as follows
service.specs.ts:
describe('SomeService', () => {
let service: SomeService;
beforeEach(() => {
service = new SomeService(
mockFilterService.object(),
mockStorageService.object(),
);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
}
On running ng-test
I get following error:
<SomeService> should be created
TypeError: this.allUsers.some is not a function
at <Jasmine>
at new BreakdownService (http://localhost:9876/_karma_webpack_/src/app/_shared/services/some.service.ts:33:48)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/_shared/services/some.service.spec.ts:19:15)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)
at <Jasmine>
at ZoneDelegate.invokeTask (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:391:1)
at Zone.runTask (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:168:1)
Expected undefined to be truthy.
error properties: Object({ originalStack: 'Error: Expected undefined to be truthy.
at new ZoneAwareError (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-error.js:100:1)
at stack (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?)
at buildExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?)
at Spec.expectationResultFactory (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?)
at Spec.addExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?)
at Expector.addExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?)
at Exp ...
Error: Expected undefined to be truthy.
at <Jasmine>
Code is running as expected and giving an output properly.
I am not able to figure out the test case error.
How do I fix this?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…