From 00543f3830285fdefd56c63814e1222c563d0b13 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 29 May 2016 20:14:27 -0400 Subject: [PATCH] fix deployment for tests --- boilerplate/test/contract_spec.js | 33 +++++++++++++++++++++++++++++++ demo/test/simple_storage_spec.js | 9 +++++++-- lib/deploy.js | 16 +++++++++++---- lib/test.js | 16 +++++++++------ 4 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 boilerplate/test/contract_spec.js diff --git a/boilerplate/test/contract_spec.js b/boilerplate/test/contract_spec.js new file mode 100644 index 000000000..5d9987df5 --- /dev/null +++ b/boilerplate/test/contract_spec.js @@ -0,0 +1,33 @@ +var assert = require('assert'); +var Embark = require('embark-framework'); +var EmbarkSpec = Embark.initTests(); +var web3 = EmbarkSpec.web3; + +describe("MyContract", function() { + // Deploy all contrats using the development config + before(function(done) { + EmbarkSpec.sim.createAccounts(10, function() { + EmbarkSpec.sim.setBalance(web3.eth.accounts[0], 1000000000000000000000, function() { + EmbarkSpec.deployAll(done); + }); + }); + }); + + //it("should set constructor value", function(done) { + // SimpleStorage.storedData(function(err, result) { + // assert.equal(result.toNumber(), 100); + // done(); + // }); + //}); + + //it("set storage value", function(done) { + // SimpleStorage.set(150, function() { + // SimpleStorage.get(function(err, result) { + // assert.equal(result.toNumber(), 150); + // done(); + // }); + // }); + //}); + +}) + diff --git a/demo/test/simple_storage_spec.js b/demo/test/simple_storage_spec.js index 916c4c5e3..392e80c68 100644 --- a/demo/test/simple_storage_spec.js +++ b/demo/test/simple_storage_spec.js @@ -1,10 +1,15 @@ var assert = require('assert'); var Embark = require('embark-framework'); var EmbarkSpec = Embark.initTests(); +var web3 = EmbarkSpec.web3; -describe("SimpleStorage", function(done) { +describe("SimpleStorage", function() { before(function(done) { - EmbarkSpec.deployAll(done); + EmbarkSpec.sim.createAccounts(10, function() { + EmbarkSpec.sim.setBalance(web3.eth.accounts[0], 1000000000000000000000, function() { + EmbarkSpec.deployAll(done); + }); + }); }); it("should set constructor value", function(done) { diff --git a/lib/deploy.js b/lib/deploy.js index d5cdac4b1..7b2e6c0be 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -31,20 +31,28 @@ Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainMa console.log("primary account address is : " + primaryAddress); }; +Deploy.waitForContract = function(transactionHash, cb) { + web3.eth.getTransactionReceipt(transactionHash, function(e, receipt) { + if (!e && receipt && receipt.contractAddress !== undefined) { + cb(receipt.contractAddress); + } + else { + Deploy.waitForContract(transactionHash, cb); + } + }); +}; + Deploy.prototype.deploy_contract = function(contractObject, contractParams, cb) { var callback = function(e, contract) { if(!e && contract.address !== undefined) { cb(contract.address); } else { - console.log("error deploying"); - console.log(e) - //exit(); + Deploy.waitForContract(contract.transactionHash, cb); } }; contractParams.push(callback); - contractObject["new"].apply(contractObject, contractParams); } diff --git a/lib/test.js b/lib/test.js index 2ba0a05d5..a3aece95b 100644 --- a/lib/test.js +++ b/lib/test.js @@ -1,20 +1,22 @@ try { - var ethersim = require('ethersim'); + var EtherSim = require('ethersim'); } catch(e) { - var ethersim = false; + var EtherSim = false; } -var web3 = require('web3'); +var Web3 = require('web3'); +var web3 Test = function(contractFiles, blockchainFile, contractFile, _env) { - if (ethersim === false) { + if (EtherSim === false) { console.log('EtherSim not found; Please install it with "npm install ethersim --save"'); console.log('For more information see https://github.com/iurimatias/ethersim'); exit(); } this.env = _env || 'development'; - this.web3 = web3; - this.web3.setProvider(ethersim.web3Provider()); + this.web3 = new Web3(); + this.sim = new EtherSim.init(); + this.web3.setProvider(this.sim.provider); this.contractFiles = contractFiles; Embark.init(this.web3); @@ -25,6 +27,7 @@ Test = function(contractFiles, blockchainFile, contractFile, _env) { } Test.prototype.deployAll = function(cb) { + var web3 = this.web3; Embark.deployContracts('development', this.contractFiles, "/tmp/abi.js", "chains.json", false, false, function(abi) { eval(abi); cb(); @@ -32,3 +35,4 @@ Test.prototype.deployAll = function(cb) { } module.exports = Test; +