简体   繁体   English

Javascript 类 getter setter 在单元测试中显示未覆盖的行

[英]Javascript class getter setter shows Uncovered Line under unit test

I have following javascript class and writing unit test using mocha and sinon.我有以下 javascript 类并使用 mocha 和 sinon 编写单元测试。 When I run test case I see uncovered lines for 'return this._agentId;'当我运行测试用例时,我看到未覆盖的“return this._agentId;”行and 'this._agentId = value;'.I am not sure how to cover these lines under test.I am using Istanbul test coverage tool to see coverage.和'this._agentId = value;'。我不确定如何覆盖这些测试行。我正在使用伊斯坦布尔测试覆盖工具来查看覆盖范围。

// Agentmessage.js // 代理消息.js

    class AgentMessage {
        constructor(agentId, message) {
            this._agentId = agentId;
            this._message = message;
        }

        get agentId() {
            return this._agentId;
        }

        set agentId(value) {
            this._agentId = value;
        }

    }

    module.exports = AgentMessage;

// Agentmessage.test.js // Agentmessage.test.js

    'use strict';

    const chai=require('chai');
    const sinon=require('sinon');
    var chaiAsPromised=require('chai-as-promised');
    chai.use(chaiAsPromised).should();
    const expect = chai.expect;
    const agentMessage = require('../src/model/agentMessage');

    describe('agentMessage test',function() {
        let sandbox;
        let agentMessageObj;
        beforeEach(() => {
            agentMessageObj = new agentMessage('agentId', 'message');
            sandbox=sinon.sandbox.create();
        });

        afterEach(() => {
            sandbox.restore();
        });
        it('agentMessage set agentId Test',() => {
            agentMessageObj.agentId = 'agentId';
            expect(agentMessageObj.agentId).to.deep.equal('agentId');
        });
        it('agentMessage get agentId Test',() => {
            expect(agentMessageObj.agentId).to.equal('agentId');
        });

    });

I am not seeing the same issue you are.我没有看到与您相同的问题。 I get 100% coverage.我得到 100% 的覆盖率。

You say istanbul but you are in fact using the nyc package correct?你说的是伊斯坦布尔,但你实际上使用的是nyc包对吗? I think you'll find that the instanbul project suggests you use the nyc runner if you are not already.我想你会发现 instanbul项目建议你使用 nyc runner,如果你还没有的话。

Consider refreshing your environment if you are able.如果可以,请考虑刷新您的环境。

rm -rf .nyc_output && rm -rf coverage && rm -rf node_modules
npm i --save-dev nyc mocha chai

If that does not clear things up, consider removing things, temporarily at least, that you are not using in these particular tests.如果这不能解决问题,请考虑至少暂时删除您在这些特定测试中未使用的内容。 sinon and chai-as-promised for example.例如 sinon 和 chai-as-promised。 Isolate the code.隔离代码。 See if there are some conflicts there.看看有没有冲突。

Try this similar code.试试这个类似的代码。 I get full coverage.我得到全面覆盖。

./node_modules/.bin/nyc --reporter html ./node_modules/.bin/mocha test.js

test.js测试.js

const { expect } = require('chai')
const AgentMessage = require('./index');

describe('agentMessage test', function () {
  let agentMessage;

  beforeEach(function () {
    agentMessage = new AgentMessage('agentId01', 'message02');
  });

  it('agentMessage set agentId Test', async function () {
    agentMessage.agentId = 'agentId02';
    expect(agentMessage.agentId).to.deep.equal('agentId02');
  });
});

If after all of that, if it is still a problem, if you're using a more advanced configuration of nyc/istanbul , start stripping away that configuration and using default properties.如果在这一切之后,如果它仍然是一个问题,如果您使用的是 nyc/istanbul更高级配置,请开始剥离该配置并使用默认属性。 See if you find the sweet/troubled part.看看你是否找到了甜蜜/麻烦的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM