Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jasmine - array.some is not a function during running ng-test when initialised in angular service

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will have to ensure that mockStorageService has a getItem method that returns an array.

Try something like this:

describe('SomeService', () => {
  let service: SomeService;

  beforeEach(() => {
    service = new SomeService(
      mockFilterService.object(),
      {
        getItem(key: string): {
          return []; // mock the array to be returned here.
        } 
      }
    );
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...