编辑:我不确定出什么问题,但是我一直在编写测试,当我进行80次测试时,错误停止出现。
我有一个角度项目设置,可以使用Karma和Jasmine进行单元测试。我最近一直在添加测试,现在每当运行70个或更多测试时,所有测试都运行后,我会收到此消息:
ERROR: 'Unhandled Promise rejection:', '<toHaveBeenCalledWith> : Expected a spy, but got Function.
Usage: expect(<spyObj>).toHaveBeenCalledWith(...arguments)', '; Zone:', 'ProxyZone', '; Task:', 'Promise.then', '; Value:', Error{}, 'Error: <toHaveBeenCalledWith> : Expected a spy, but got Function.
Usage: expect(<spyObj>).toHaveBeenCalledWith(...arguments)
at compare (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?916005cc407925f4764668d61d04888d59258f5d:3340:17)
at Expectation.toHaveBeenCalledWith (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?916005cc407925f4764668d61d04888d59258f5d:1543:35)
at http://localhost:9876/_karma_webpack_/webpack:/Users/cmaxfield/workspace/carbon-angular/src/app/select/select.component.spec.ts:73:36
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/Users/cmaxfield/workspace/carbon-angular/node_modules/zone.js/dist/zone.js:388:1)
at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/Users/cmaxfield/workspace/carbon-angular/node_modules/zone.js/dist/proxy.js:128:1)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/Users/cmaxfield/workspace/carbon-angular/node_modules/zone.js/dist/zone.js:387:1)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/Users/cmaxfield/workspace/carbon-angular/node_modules/zone.js/dist/zone.js:138:1)
at http://localhost:9876/_karma_webpack_/webpack:/Users/cmaxfield/workspace/carbon-angular/node_modules/zone.js/dist/zone.js:872:1
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:9876/_karma_webpack_/webpack:/Users/cmaxfield/workspace/carbon-angular/node_modules/zone.js/dist/zone.js:421:1)
at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvokeTask (http://localhost:9876/_karma_webpack_/webpack:/Users/cmaxfield/workspace/carbon-angular/node_modules/zone.js/dist/proxy.js:159:1)'
我的所有测试都成功执行,并且因果报应完成,没有出现任何问题,即使出现此错误:
Chrome 67.0.3396 (Mac OS X 10.12.6): Executed 70 of 70 SUCCESS (3.541 secs / 3.246 secs)
TOTAL: 70 SUCCESS
如果我甚至注释掉一个测试(与哪个测试都没有关系),那么该错误消息将不再出现,并且我无法弄清楚发生了什么。
这是我的karma.conf.js
:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-phantomjs-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-junit-reporter'),
require('karma-spec-reporter'),
require('karma-coverage'),
require('karma-remap-coverage'),
require('@angular/cli/plugins/karma'),
],
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
},
angularCli: {
environment: 'dev',
},
reporters: ['junit', 'spec'],
junitReporter: {
outputDir: 'out', // results will be saved as $outputDir/$browserName.xml
outputFile: undefined, // if included, results will be saved as $outputDir/$browserName/$outputFile
suite: '', // suite will become the package name attribute in xml testsuite element
useBrowserName: false, // add browser name to report and classes names
nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
properties: {}, // key value pair of properties to add to the <properties> section of the report
xmlVersion: null, // use '1' if reporting to be per SonarQube 6.2 XML format
},
specReporter: {
maxLogLines: 5, // limit number of lines logged per test
suppressErrorSummary: true, // do not print error summary
suppressFailed: false, // do not print information about failed tests
suppressPassed: false, // do not print information about passed tests
suppressSkipped: false, // do not print information about skipped tests
showSpecTiming: false, // print the time elapsed for each spec
},
coverageReporter: {
type: 'in-memory',
},
remapCoverageReporter: {
'text-summary': null,
json: './coverage/coverage.json',
cobertura: './coverage/cobertura.xml',
html: './coverage/html',
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: false,
});
};
答案 0 :(得分:0)
在继续添加测试并达到80个测试之后,问题得以解决,不确定为什么随机的测试数量会出现问题。