From bc0803853d58e00e865cf29c46984924f64d3362 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Sat, 17 Mar 2018 20:36:28 -0400 Subject: [PATCH] Updating documentation related to smart contracts and tests --- docs/tests.rst | 49 ++++++++++++++++++++++++++++++++++++++-- docs/using-contracts.rst | 23 ++++++++++++++++++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/docs/tests.rst b/docs/tests.rst index 0172d99b..08dac4db 100644 --- a/docs/tests.rst +++ b/docs/tests.rst @@ -1,8 +1,8 @@ Testing Ethereum Contracts ========================== -You can run specs with ``embark test``, it will run any test files under -``test/``. +You can run specs with ``embark test``, it will run all the test files under +``test/``. You can run a specific test file with ``embark test Embark includes a testing lib to fastly run & test your contracts in a EVM. @@ -120,3 +120,48 @@ and initialize the testing functionality. Below is an example using mocha: ``mocha test/simple_storage_spec.js --no-timeouts`` +**accessing accounts** + +The callback used in the function ``deployAll`` will let you access the accounts configured in the EVM. +You can assign them to a variable and use them in your tests. + +.. code:: javascript + + var accountArr; + EmbarkSpec.deployAll(contractsConfig, (accounts) => { + accountArr = accounts; + done(); + }); + +**creating contract instances in your tests** + +Embark handles the deployment of your contracts through the function ``deployAll``. +However you can use ``web3.eth.contract`` for creating instances of your contracts manually. + +.. code:: javascript + + var simpleStorageJson = require('../dist/contracts/SimpleStorage.json'); + var accountArr; + var simpleStorage; + + describe("SimpleStorage", function() { + this.timeout(0); + before(function(done) { + EmbarkSpec.deployAll({}, (accounts) => { + accountArr = accounts; + done(); + }); + }); + + it("should deploy a contract instance", function(done) { + var simpleStorageContract = new web3.eth.Contract(simpleStorageJson.abi); + simpleStorageContract.deploy({data: simpleStorageJson.code, arguments: [100]}) + .send({from: accountArr[0], gas: 5000000, gasPrice: 1}) + .then(function(contractInstance){ + simpleStorage = contractInstance; + simpleStorage.setProvider(web3.currentProvider); + assert(simpleStorage.options.address != null); + }) + .finally(done); + }); + }); diff --git a/docs/using-contracts.rst b/docs/using-contracts.rst index 2a2ea769..3f5e6ac3 100644 --- a/docs/using-contracts.rst +++ b/docs/using-contracts.rst @@ -47,7 +47,8 @@ arguments: "args": [ 100 ], - "gas": 800000 + "gas": 800000, + "gasPrice": 5 } } } @@ -177,3 +178,23 @@ instead. ... } +You can specify actions to do after the deployment of a contract using the "onDeploy" parameter. + +| "onDeploy" - should be an array of javascript instructions that will be evaluated and executed + +.. code:: json + + # config/contracts.json + { + "development": { + "gas": "auto", + "contracts": { + "SimpleStorage": { + "args": [ + 100 + ], + "onDeploy": ["SimpleStorage.methods.set(150).send()"] + } + } + } + } \ No newline at end of file