使用mocha的单元测试coverage socket.io

huus2vyu  于 2021-06-10  发布在  Redis
关注(0)|答案(0)|浏览(290)

您好,我正在为socket.io编写单元测试用例,测试用例通过了,但没有反映出我应该如何解决这个问题。为了更多的参考,我发布了下面的代码,这里是我的代码索引.js

const app = require('express')();
const server = require('http').Server(app);
const redis = require('redis');

var client = redis.createClient(process.env.redisPort,process.env.redisHost);
var client1 = redis.createClient(process.env.redisPort,process.env.redisHost);
var client2 = redis.createClient(process.env.redisPort,process.env.redisHost);
var client3 = redis.createClient(process.env.redisPort,process.env.redisHost);
const io = require('socket.io')(server);
const port = parseInt(process.env.PORT, 10) || 35082;
server.listen(port);

app.get('/', function (req, res) {
  res.send('ws server');
});

io.sockets
  .on('connection', 
  (socket) => {
    console.log("Socket Connected");
    client.on('message',function(channel,msg){
      socket.emit('pickedup',msg);
    });
    client.subscribe('pickedup');

    client1.on('message',function(channel,msg){
      socket.emit('order-da-issue',msg);
    });
    client1.subscribe('da-issue');

    client2.on('message',function(channel,msg){
      socket.emit('crew-issue',msg);
    });
    client2.subscribe('crew-issue');

    client3.on('message',function(channel,msg){
      socket.emit('da-break',msg);
    });
    client3.subscribe('da-break');
  });

这是我的index.test.js实际上它没有做任何核心测试它只测试套接字连接。

var io = require('socket.io-client'),
    assert = require('assert'),
    expect = require('expect.js');
describe('Suite of unit tests', function () {
    var socket;

    beforeEach(function (done) {
        socket = io.connect('http://localhost:35082', {
            'reconnection delay': 0,
            'reopen delay': 0,
            'force new connection': true
        });
        socket.on('connect', function () {
            console.log('worked...');
            done();
        });
        socket.on('disconnect', function () {
            console.log('disconnected...');
        })
    });
    afterEach(function (done) {
        if (socket.connected) {
            console.log('disconnecting...');
            socket.disconnect();
        } else {
            console.log('no connection to break...');
        }
        done();
    });

    describe('First (hopefully useful) test', function () {
        it('Doing something else with indexOf()', function (done) {
            expect([1, 2, 3].indexOf(5)).to.be.equal(-1);
            expect([1, 2, 3].indexOf(0)).to.be.equal(-1);
            done();
        });

    });

});

这是我的package.json

"test": "NODE_ENV=test mocha ./test/*.test.js --exit",
"coverage": "NODE_ENV=test nyc --reporter=json-summary --all --reporter=text --reporter=html --report-dir=./coverage npm test",
"nyc": "nyc --reporter=json-summary --reporter=text --reporter=html --report-dir=./coverage npm run test"

这是测试覆盖率结果

Suite of unit tests
    First (hopefully useful) test
worked...
      ✓ Doing something else with indexOf()
disconnecting...
disconnected...

  1 passing (144ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
 index.js |       0 |        0 |       0 |       0 | 1-124             
----------|---------|----------|---------|---------|-------------------

谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题