2

我很难开始为我的应用程序编写单元测试。我的覆盖率总是报告 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)
4

2 回答 2

2

测试覆盖率表示被测试源的百分比,而不是通过测试的百分比。

因为您在 test.js 中有两个函数,并且每个函数都有一个测试,所以覆盖率是 100%。

但是,实际上只有一项测试通过了。所以你的成功率是50%。

于 2015-12-31T15:25:30.440 回答
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: {}
于 2015-12-31T16:29:25.347 回答