命名空间“ jasmine”没有导出成员“ SpyObj”

时间:2018-09-17 08:34:57

标签: angular jasmine

我正在尝试为测试创建间谍对象:

let spy: jasmine.SpyObj<GeneralService>;

不幸的是,运行ng test时出现此错误:

Namespace 'jasmine' has no exported member 'SpyObj'

此外,WebStorm将SpyObj标记为未解析的变量。

这是我的devDependencies中的package.json

"devDependencies": {
    "@angular/cli": "1.7.3",
    "@types/jasmine": "2.5.36",
    "@types/node": "7.0.27",
    "jasmine": "~2.4.1",
    "jasmine-core": "~2.4.1",
    "karma": "^1.7.1",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-phantomjs-launcher": "^1.0.2",
    "lodash": "4.16.2",
    "ts-node": "1.3.0",
    "tslint": "^3.15.1",
    "typescript": "2.6.2"
  }

更新:

我参考Angular测试文档: https://angular.io/guide/testing

let masterService: MasterService;
let valueServiceSpy: jasmine.SpyObj<ValueService>;

beforeEach(() => {
  const spy = jasmine.createSpyObj('ValueService', ['getValue']);

  TestBed.configureTestingModule({
    // Provide both the service-to-test and its (spy) dependency
    providers: [
      MasterService,
      { provide: ValueService, useValue: spy }
    ]
  });
  // Inject both the service-to-test and its (spy) dependency
  masterService = TestBed.get(MasterService);
  valueServiceSpy = TestBed.get(ValueService);
});

我最终将间谍配置为any

 let spy: any;

1 个答案:

答案 0 :(得分:0)

您的茉莉花版本不支持在更高版本中引入的SpyObj类型。

尝试将jasmine-core@types/jasminepackage.json更新到最新的稳定版本,或者尝试使用"jasmine-core": "2.8.0", @types/jasmine": "2.6.0"都可以正常工作而没有错误。