test.method.js in helpers

This commit is contained in:
Marek Kotewicz 2015-03-24 06:50:42 +01:00
parent 7df9099075
commit c1f5ab2cdc
3 changed files with 70 additions and 104 deletions

View File

@ -1,7 +1,6 @@
var chai = require('chai');
var assert = chai.assert;
var BigNumber = require('bignumber.js');
var web3 = require('../index');
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var testMethod = require('./helpers/test.method.js');
var method = 'getBalance';
@ -9,73 +8,27 @@ var tests = [{
args: [301, 2],
formattedArgs: ['0x12d', '0x2'],
result: '0x31981',
formattedResult: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
},{
args: ['0x12d', '0x1'],
formattedArgs: ['0x12d', '0x1'],
result: '0x31981',
formattedResult: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
}, {
args: [0x12d, 0x1],
formattedArgs: ['0x12d', '0x1'],
result: '0x31981',
formattedResult: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
}, {
args: [0x12d],
formattedArgs: ['0x12d', web3.eth.defaultBlock],
result: '0x31981',
formattedResult: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
}];
describe('eth', function () {
describe(method, function () {
tests.forEach(function (test, index) {
it('sync test: ' + index, function () {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
});
// when
var result = web3.eth[method].apply(null, test.args.slice(0));
// then
assert.equal(+test.formattedResult, result.toNumber());
});
it('async test: ' + index, function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
});
var callback = function (err, result) {
assert.equal(+test.formattedResult, result.toNumber());
done();
};
var args = test.args.slice(0);
args.push(callback);
// when
web3.eth[method].apply(null, args);
});
});
});
});
testMethod.runTests(method, tests);

View File

@ -1,8 +1,6 @@
var chai = require('chai');
var assert = chai.assert;
var web3 = require('../index');
var BigNumber = require('bignumber.js');
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var testMethod = require('./helpers/test.method.js');
var method = 'getBlock';
@ -137,51 +135,5 @@ var tests = [{
call: 'eth_'+ method + 'ByNumber'
}];
describe('eth', function () {
describe(method, function () {
tests.forEach(function (test, index) {
it('sync test: ' + index, function () {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
});
// when
var result = web3.eth[method].apply(null, test.args.slice(0));
// then
assert.deepEqual(test.formattedResult, result);
});
it('async test: ' + index, function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
});
var callback = function (err, result) {
assert.deepEqual(test.formattedResult, result);
done();
};
var args = test.args.slice(0);
args.push(callback);
// when
web3.eth[method].apply(null, args);
});
});
});
});
testMethod.runTests(method, tests);

View File

@ -0,0 +1,61 @@
var chai = require('chai');
var assert = chai.assert;
var web3 = require('../../index');
var FakeHttpProvider = require('./FakeHttpProvider');
var runTests = function (method, tests) {
describe('eth', function () {
describe(method, function () {
tests.forEach(function (test, index) {
it('sync test: ' + index, function () {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
});
// when
var result = web3.eth[method].apply(null, test.args.slice(0));
// then
assert.deepEqual(test.formattedResult, result);
});
it('async test: ' + index, function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
});
var callback = function (err, result) {
assert.deepEqual(test.formattedResult, result);
done();
};
var args = test.args.slice(0);
args.push(callback);
// when
web3.eth[method].apply(null, args);
});
});
});
});
};
module.exports = {
runTests: runTests
}