Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

unit-testing - 用socket.io npm编写聊天应用程序的单元测试(write unit test for chat app with socket.io npm , jest)

I tried to write unit test and create a mock socket object and when I run test it passes but it doesn't touch any of my app functions.

(我试图编写单元测试并创建一个模拟套接字对象,当我运行测试时,它通过了,但没有涉及到我的任何应用程序功能。)

It doesn't cover any function.

(它不包含任何功能。)

Help me to write proper unit test for my app.

(帮助我为我的应用编写适当的单元测试。)

I tried to write unit test and create a mock socket object and when I run test it passes but it doesn't touch any of my app functions.

(我试图编写单元测试并创建一个模拟套接字对象,当我运行测试时,它通过了,但没有涉及到我的任何应用程序功能。)

It doesn't cover any function.

(它不包含任何功能。)

Help me to write proper unit test for my app.

(帮助我为我的应用编写适当的单元测试。)

app.js

(app.js)


const io = require('socket.io')(3000);

io.on('connection', (socket) => {
  socket.on('username', (data) => {
    socket.username = data.username;
    console.log(`${socket.username} joined the chat!`);

    socket.broadcast.emit('new user', socket.username);
  });

  socket.on('message', (data) => {
    socket.broadcast.emit('message', {user: socket.username, message: data});
  });

  socket.on('disconnect', () => {
    console.log(`User left the chat`);
  });
});

chat.js

(chat.js)


const io = require('socket.io-client');
const socket = io.connect('http://localhost:3000');

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let username;

socket.on('connect', () => {
  console.log(`Chat started
`);

  rl.question('What is your name? ', (answer) => {
    username = answer;
    socket.emit('username', {username: username});
  });
});

rl.on('line', (message) => {
  socket.emit('message', message);
});

socket.on('message', (data) => {
  console.log(`${data.user}: ${data.message}`);
});

socket.on('new user', (data) => {
  console.log(`${data} joined the chat
`);
});



my app test

(我的应用程式测试)

const io = require('../server/app.js');
const MockedSocket = require('socket.io-mock');
//let socket = new MockedSocket();

//const  SocketMock = 'socket.io-mock';
const { expect } = require('chai');

describe('socket tests', function() {
  it('Sockets should be able to talk to each other without a server', function(done){
    let socket = new MockedSocket();
    socket.on('message', function (message) {
      expect(message).to.equal('Hello World!');
    });
    socket.socketClient.emit('message', 'Hello World!');
    jest.setTimeout(30000);
    done();
  });

  it('it console logs when user is connect', () => {
    let socket = new MockedSocket();
    let username = 'test';
    socket.on('user name', function(data){
      socket.username = data.username;
      socket.socketClient.emit('new user', username);
      expect(data).to.equal('test joined the chat!');

    });

  });

});```

I tried to write unit test and create a mock socket object and when I run test it passes but it doesn't touch any of my app functions. It doesn’t cover any function. Help me to write proper unit test for my app.

I tried to write unit test and create a mock socket object and when I run test it passes but it doesn't touch any of my app functions. It doesn’t cover any function. Help me to write proper unit test for my app.
  ask by user11399400 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...