Authored by 周奇琪

编写connect.js的单元测试

... ... @@ -39,7 +39,7 @@ class Connector extends events.EventEmitter {
client.connect(config.port, config.host, function() {
that.connected = true;
log.info(config.host + ':' + config.port + ':读写器连接成功!');
send();
send(that);
});
... ...
... ... @@ -26,7 +26,7 @@ export let log = loger;
* @param {Number} number 十进制数
* @return {Number} 16进制数字
*/
export let tenToHex = function (number) {
export let tenToHex = function(number) {
return (parseInt(number, 10) >>> 0).toString(16);
};
... ... @@ -37,7 +37,7 @@ export let tenToHex = function (number) {
* @param {Boolean} minus true表示负数,false表示正数
* @return {Number} 十进制数字
*/
export let hexToTen = function (number, minus) {
export let hexToTen = function(number, minus) {
if (minus) {
return parseInt('FFFF' + number, 16) >> 0;
} else {
... ... @@ -52,7 +52,7 @@ export let hexToTen = function (number, minus) {
* @param {Buff} data 返回数据流
* @return {Array} 转成16进制的数组
*/
export let toHex = function (data) {
export let toHex = function(data) {
let arr = [];
for (let i = 0; i < data.length; i++) {
arr[i] = tenToHex(data.readUIntLE(i));
... ... @@ -69,7 +69,7 @@ export let toHex = function (data) {
* @param {Object} output 标准输出的规格对象
* @return {object} 标准输出
*/
export let returnProc = function (data, ip, output) {
export let returnProc = function(data, ip, output) {
if (!_.isArray(data) || _.size(data) < 1) {
return false;
}
... ... @@ -78,7 +78,7 @@ export let returnProc = function (data, ip, output) {
if (!output[ret]) {
return false;
}
//log.info("读写器(" + ip + ")返回:" + output[ret].message + " 返回码:" + ret);
log.info("读写器(" + ip + ")返回:" + output[ret].message + " 返回码:" + ret);
output[ret].hex = data;
return output[ret];
};
... ... @@ -90,7 +90,7 @@ export let returnProc = function (data, ip, output) {
* @param {String} data 得到的指令数据结果集
* @return {Object} 返回拆分结果和拆分依据的指令头
*/
export let spliteRet = function (mq, data) {
export let spliteRet = function(mq, data) {
let indexes = [],
rets = [],
i,
... ... @@ -147,9 +147,9 @@ export let spliteRet = function (mq, data) {
* @param {Array} data 返回的结果集 eg. ['f0','2','0','0']
* @return {Object} 返回定位到的指令
*/
export let locateOrder = function (orders, data) {
export let locateOrder = function(orders, data) {
let orderRet = {};
_.forIn(orders, function (val, key) {
_.forIn(orders, function(val, key) {
var order = val.input.toLowerCase();
var prefix = data[0].toLowerCase();
... ...
import expect from 'expect.js';
import muk from 'muk';
import net from '../mock/fakeNet';
describe('connector.js', function() {
let C;
before(function() {
C = muk('../../src/lib/connector', {
'net': net
});
C.__set__('log', {
info: function() {},
error: function() {}
});
})
describe('init', function() {
it('new connector init ok', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {
expect(con.connected).to.be(true);
done();
});
});
it('new connector error', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function(client, data, error) {
expect(error.message).to.be('mock error');
done();
});
connect.client.addEmit('error', {
message: 'mock error'
});
});
it('new connect after error then reconnect', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {});
connect.on('reconnect', function() {
done();
});
connect.client.addEmit('error', {
message: 'mock error'
});
});
});
describe('connector write then get data', function() {
it('on data expect ok', function(done) {
C.__set__('toHex', function() {});
C.__set__('spliteRet', function() {
return {
rets: ['mock']
}
});
C.__set__('locateOrder', function() {
return {
name: 'mock'
}
});
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {
return 'ok';
});
connect.on('mock', function(data) {
expect(data).to.be('ok');
done();
});
connect.client.write('test');
});
});
describe('sendOrder', function() {
it('send expect same back', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {});
C.__set__('spliteRet', function(mq) {
return {
rets: [],
mq: mq
};
});
let call = new Buffer('Mock', 'hex');
connect.client.on('data', function(data) {
expect(data).to.eql(call);
expect(connect.orderMq[0]).to.be('mo');
done();
});
connect.send('Mock');
});
})
after(function() {
muk.restore();
});
});
\ No newline at end of file
... ...
import expect from 'expect.js';
import muk from 'muk';
\ No newline at end of file
... ...
import events from 'events';
class FakeSocket extends events.EventEmitter {
constructor() {
super();
this.data = {};
}
connect(port, host, callback) {
callback();
}
write(data) {
this.addEmit('data', data);
}
addEmit(ev, data) {
let that = this;
process.nextTick(function() {
that.emit(ev, data);
});
}
destroy() {
let that = this;
process.nextTick(function() {
that.addEmit('close', that.data);
});
}
}
export default {
Socket: FakeSocket
};
\ No newline at end of file
... ...
... ... @@ -2,4 +2,5 @@ require("babel/register")({
plugins: ["babel-plugin-rewire"]
}); //es6 polyfill
require('./lib/index.test');
\ No newline at end of file
require('./lib/index.test');
require('./lib/connector.test');
\ No newline at end of file
... ...