diff --git a/lib/tests/test.js b/lib/tests/test.js index 4e2b8e2f7..3ac0b2d47 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -132,6 +132,9 @@ class Test { }, function createContractObject(next) { async.each(Object.keys(self.contracts), (contractName, eachCb) => { + if (!self.engine.contractsManager.contracts[contractName]) { + throw new Error(__('No contract with the name %s', contractName)); + } const contract = self.engine.contractsManager.contracts[contractName]; Object.assign(self.contracts[contractName], new self.web3.eth.Contract(contract.abiDefinition, contract.address, {from: self.web3.eth.defaultAccount, gas: 6000000})); @@ -150,13 +153,29 @@ class Test { require(module) { if (module.startsWith('Embark/contracts/')) { const contractName = module.substr(17); - if (!this.engine.contractsManager.contracts[contractName]) { - throw new Error(__('No contract with the name %s', contractName)); - } if (this.contracts[contractName]) { return this.contracts[contractName]; } - const contract = this.engine.contractsManager.contracts[contractName]; + let contract = this.engine.contractsManager.contracts[contractName]; + if (!contract) { + const contractNames = Object.keys(this.engine.contractsManager.contracts); + // It is probably an instanceof + contractNames.find(contrName => { + // Find a contract with a similar name + if (contractName.indexOf(contrName) > -1) { + contract = this.engine.contractsManager.contracts[contrName]; + return true; + } + return false; + }); + // If still nothing, assign bogus one, we will redefine it anyway on deploy + if (!contract) { + console.warn(__('Could not recognize the contract name "%s"')); + console.warn(__('If it is an instance of another contract, it will be reassigned on deploy')); + console.warn(__('Otherwise, you can rename the contract to contain the parent contract in the name eg: Token2 for Token')); + contract = this.engine.contractsManager.contracts[contractNames[0]]; + } + } this.contracts[contractName] = new this.web3.eth.Contract(contract.abiDefinition, contract.address, {from: this.web3.eth.defaultAccount, gas: 6000000}); return this.contracts[contractName]; diff --git a/test_apps/contracts_app/test/array_references_spec.js b/test_apps/contracts_app/test/array_references_spec.js index 99cf37da9..719b5146b 100644 --- a/test_apps/contracts_app/test/array_references_spec.js +++ b/test_apps/contracts_app/test/array_references_spec.js @@ -1,28 +1,33 @@ +/*global contract, config, it, embark*/ +const assert = require('assert'); +const SomeContract = embark.require('Embark/contracts/SomeContract'); +const SimpleStorage = embark.require('Embark/contracts/SimpleStorage'); +const MyToken2 = embark.require('Embark/contracts/MyToken2'); + +config({ + contracts: { + "SimpleStorage": { + args: [100] + }, + "Token": { + deploy: false, + args: [1000] + }, + "MyToken2": { + instanceOf: "Token", + args: [2000] + }, + "SomeContract": { + "args": [ + ["$MyToken2", "$SimpleStorage"], + 100 + ] + } + } +}); + contract("SomeContract", function() { this.timeout(0); - before(function(done) { - this.timeout(0); - var contractsConfig = { - "SimpleStorage": { - args: [100] - }, - "Token": { - deploy: false, - args: [1000] - }, - "MyToken2": { - instanceOf: "Token", - args: [2000] - }, - "SomeContract": { - "args": [ - ["$MyToken2", "$SimpleStorage"], - 100 - ] - } - }; - EmbarkSpec.deployAll(contractsConfig, () => { done() }); - }); it("set MyToken2 address", async function() { let address = await SomeContract.methods.addr_1().call();