【问题标题】:array.some is not a function during running ng-test when initialised in angular service在角度服务中初始化时,array.some 在运行 ng-test 期间不是函数
【发布时间】:2020-12-30 23:08:38
【问题描述】:

我通过从缓存中获取 allUsers 来初始化它,然后使用 allUsers.some() (Array.prototype.some() for reference) 从 allUsers 将布尔值分配给 specialUserExists

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,
  ) { }
}

service.specs.ts我写了测试用例如下 service.specs.ts

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

  beforeEach(() => {
    service = new SomeService(
      mockFilterService.object(),
      mockStorageService.object(),
    );
  });

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

在运行ng-test 时出现以下错误:

<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>

代码按预期运行并正确输出。 我无法弄清楚测试用例错误。

我该如何解决这个问题?

【问题讨论】:

  • this.storageService.getItem('SOME_KEY') 可能没有返回数组。
  • service.ts 中的@Celsiuss 工作正常,但在service.specs.ts 中如何初始化它?

标签: angular jasmine karma-jasmine angular-test


【解决方案1】:

您必须确保mockStorageService 具有返回数组的getItem 方法。

试试这样的:

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();
  });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-05
    • 2014-09-18
    • 2019-03-25
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多