我很难开始为我的应用程序编写单元测试。 我的报道总是报告100%。我做了简单的测试,结果应该是50%,但业力覆盖率并不这么认为。如果业力进展报告正确的结果,但我需要使覆盖工作。
这是我的karma.conf.coffee:
module.exports = (config) ->
config.set
basePath: ""
frameworks: ["jasmine"]
files: [
#"server/*_spec.js",
#"server/*.js",
#"client/app/final/.js"
#"client/*_spec.js"
"test.js"
"test_spec.js"
]
exclude: []
preprocessors: {}
reporters: ["progress", "coverage"]
preprocessors:
"test.js": ["coverage"]
coverageReporter:
type : "html",
dir : "coverage/"
port: 9876
colors: true
logLevel: config.LOG_INFO
autoWatch: false
browsers: [
"Chrome"
#"Firefox" add to package.json::devDependencies:["karma-firefox-launcher"]
#"PhantomJS" add to package.json::dependencies:["phantomjs"] and package.json::devDependencies["karma-phantomjs-launcher"]
]
singleRun: true
concurrency: Infinity
这是test.js:
function test(a) {
return a
}
function ivo() {
return "ivo"
}
最后是test_spec.js
describe("test", function() {
it("it tests", function() {
expect(test(3)).toEqual(3);
});
});
describe("ivo", function() {
it("it ivos", function() {
expect(ivo()).toEqual("ovi");
});
});
业力进展结果:
karma start
31 12 2015 17:12:48.598:INFO [karma]: Karma v0.13.16 server started at http://localhost:9876/
31 12 2015 17:12:48.610:INFO [launcher]: Starting browser Chrome
31 12 2015 17:12:50.007:INFO [Chrome 47.0.2526 (Linux 0.0.0)]: Connected on socket q97o45wS4ctU_z4DAAAA with id 35753464
Chrome 47.0.2526 (Linux 0.0.0) ivo it ivos FAILED
Expected 'ivo' to equal 'ivo1'.
at Object.<anonymous> (/home/ivo/ivo/diplomna3/test_spec.js:9:19)
Chrome 47.0.2526 (Linux 0.0.0): Executed 2 of 2 (1 FAILED) (0.004 secs / 0.01 secs)
答案 0 :(得分:2)
测试覆盖率表示测试源的百分比,而不是通过测试的百分比。
因为test.js中有两个函数,并且每个函数都有一个测试,所以覆盖率为100%。
然而,实际上只有一个测试通过。所以你的成功率是50%。
答案 1 :(得分:1)
这是错误
karma start --log-level=DEBUG
31 12 2015 18:24:29.017:DEBUG [plugin]: Loading plugin karma-spec-reporter.
31 12 2015 18:24:29.024:DEBUG [plugin]: Loading plugin karma-coverage.
/home/ivo/ivo/diplomna3/node_modules/di/lib/injector.js:9
throw error('No provider for "' + name + '"!');
^
Error: No provider for "framework:jasmine"! (Resolving: framework:jasmine)
at error (/home/ivo/ivo/diplomna3/node_modules/di/lib/injector.js:22:12)
at Object.parent.get (/home/ivo/ivo/diplomna3/node_modules/di/lib/injector.js:9:13)
at [object Object].get (/home/ivo/ivo/diplomna3/node_modules/di/lib/injector.js:54:19)
at /home/ivo/ivo/diplomna3/node_modules/karma/lib/server.js:137:20
at Array.forEach (native)
at [object Object].Server._start (/home/ivo/ivo/diplomna3/node_modules/karma/lib/server.js:136:21)
at [object Object].invoke (/home/ivo/ivo/diplomna3/node_modules/di/lib/injector.js:75:15)
at [object Object].Server.start (/home/ivo/ivo/diplomna3/node_modules/karma/lib/server.js:101:18)
at Object.exports.run (/home/ivo/ivo/diplomna3/node_modules/karma/lib/cli.js:231:26)
at requireCliAndRun (/usr/local/lib/node_modules/karma-cli/bin/karma:44:16)
at /usr/local/lib/node_modules/karma-cli/bin/karma:54:12
at /usr/local/lib/node_modules/karma-cli/node_modules/resolve/lib/async.js:44:21
at ondir (/usr/local/lib/node_modules/karma-cli/node_modules/resolve/lib/async.js:187:31)
at /usr/local/lib/node_modules/karma-cli/node_modules/resolve/lib/async.js:153:39
at onex (/usr/local/lib/node_modules/karma-cli/node_modules/resolve/lib/async.js:93:22)
at /usr/local/lib/node_modules/karma-cli/node_modules/resolve/lib/async.js:24:18
at FSReqWrap.oncomplete (fs.js:83:15)
这是我在配置中更改的内容:
reporters: ["spec", "coverage"]
plugins: ["karma-spec-reporter", "karma-coverage"]
preprocessors:
"test.js": ["coverage"]
specReporter: {}