在套接字测试用例中,我如何检查socket.on()是否正常工作

时间:2018-01-29 12:27:49

标签: node.js socket.io mocha chai

socket.test.js

var io = socketio;
var socketObj = require('../util/socket.service');
var should = require("chai").should();
var options = {
transports: ['websocket'],
     'force new connection': true,
    'reconnection': false
};
var socket = null;
describe('chatService', function() {
   //Test for send message
   it('send message', function() {
      var message = {
         _id: null,
        receiverId: 2,
        receiverType: 'personal', 
        senderId: 1, 
        picUrl: null,
        text: 'hey',
        type: 'text',
        status: 'avaliable',
        contentType: 'text',
        contentData: {
            data: ['adff']
        },
        responseData: {
            data: ['aggf']
        },
        createdBy: 'arun',
        updatedBy: 'arun'
    };
    var user = { id: 30 };
    var socket = io.connect('http://localhost:3000');
     //message is printing on console.
    console.log(message);
    socket.on('send-message', function(message) {
     //message is not printing on console.
        console.log(message);
        socket.emit('receive-message', msg);
        expect(message).to.equal(msg);
       });
    });
 });

这里用mocha编写测试用例发送消息,它会将我的硬编码消息从这里发送到socket.service,receive-message将从服务器返回。 但问题是,无论我在socket.on()中编写console.log()是不是打印,所以如何检查socket.on是否正常工作。

socket.service.js

import MessageService from '../apis/message/message.service';
import GroupService from '../apis/group/group.service';
import log from '../config/log4js.config';
import UserService from '../apis/user/user.service';
import UserModel from '../apis/user/index';

var messageService = new MessageService();
var groupService = new GroupService();
var userService = new UserService();

exports.connectSocket = (io) => {
   io.on('connection', function(socket) {

      // get userId from client
      socket.on('user-connected', userId => {
        console.log('a user connected with ID: ' + userId);

        userService.getById(userId, (user) => {
            if (user.id === userId) {
                userService.updateRegisteredUser({ 'id': userId, 'socketId': 
     socket.id }, (user) => {});
            }
        });

        //user disconnected
        socket.on('disconnect', () => {
            console.log('user disconnected with ID: ' + userId);
        });
    });

    /**
     * for sending message to group/user which is emitted from client(msg 
    with an groupId/userId)
     */
    socket.on('send-message', (msg) => {
        // if it is a group message
        if (msg.receiverType === "group") {
            userService.getById(msg.senderId, (result) => {
                msg.createdBy = result.name;
                msg.picUrl = result.picUrl;
                messageService.sendMessage(msg, (result) => {
                    console.log('message: ', msg);
                });
            });
            groupService.getAllUsersByGroupId(msg.receiverId)
                .then((users) => {
                    users.map(user => {
                        io.in(user.socketId).emit('receive-message', msg); 
    //emit one-by-one for all users
                    });
                });
        }
        // if it is a private message 
        else if (msg.receiverType === "private") {
            userService.getById(msg.senderId, (result) => {
                msg.createdBy = result.name;
                msg.picUrl = result.picUrl;
                messageService.sendMessage(msg, (result) => {});
            });
            userService.getById(msg.receiverId, (result) => {
                socket.to(result.socketId).emit('receive-message', msg);
            })
        }
        // if neither group nor user is selected
        else {
            userService.getById(msg.senderId, (result) => {
                socket.to(result.socketId).emit('receive-message', { "text": 
  'Select a group or an user to chat with.' }); //only to sender
            });
        }
      });
    });
  }

这是我的服务器代码。

0 个答案:

没有答案