Skip to content

Commit

Permalink
add async error test
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed May 13, 2015
1 parent d02a375 commit 55553d7
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions test/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var chai = require('chai');
var assert = chai.assert;
var web3 = require('../index');
var FakeHttpProvider = require('./helpers/FakeHttpProvider');

// use sendTransaction as dummy
var method = 'sendTransaction';

var tests = [{
result: '0xb',
formattedResult: '0xb',
call: 'eth_'+ method
}];

describe('async', function () {
tests.forEach(function (test, index) {
it('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, [{}]);
});

// when
web3.eth[method]({}, function(error, result){

// then
assert.isNull(error);
assert.strictEqual(test.formattedResult, result);

done();
});

});

it('error test: ' + index, function (done) {

// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectError({
message: test.result,
code: -32603
});
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, [{}]);
});

// when
web3.eth[method]({}, function(error, result){

// then
assert.isUndefined(result);
assert.strictEqual(test.formattedResult, error.message);

done();
});

});
});
});

0 comments on commit 55553d7

Please sign in to comment.