truffle 部署合约

truffle init webpack
目录:
这里写图片描述

创建合约
在./contracts目录下创建一个新的合约,起名为 SimpleStorage.sol,并添加以下代码:

    pragma solidity ^0.4.0;    
    contract SimpleStorage {    
        function multiply(uint a) returns(uint d) {  
            return a * 7;  
        }  
    }   

添加配置:
在./migrations目录下,打开 2_deploy_contracts.js 文件,并进行修改。

var ConvertLib = artifacts.require(“./ConvertLib.sol”);
var MetaCoin = artifacts.require(“./MetaCoin.sol”);
var SimpleStorage = artifacts.require(“./SimpleStorage.sol”);

module.exports = function(deployer) {
deployer.deploy(ConvertLib);
deployer.link(ConvertLib, MetaCoin);
deployer.deploy(MetaCoin);
deployer.deploy(SimpleStorage);
};

添加测试:
在./test目录下,新建 TestSimpleStorage.sol,用于测试上述合约。在Truffle中,可使用.js与.sol来测试合约,这里我仅使用.sol进行测试。

    pragma solidity ^0.4.0;  

    import "truffle/Assert.sol";  
    import "truffle/DeployedAddresses.sol";  
    import "../contracts/SimpleStorage.sol";  

    contract TestSimpleStorage {    
        uint someValue;  
        uint value;  
        function testmultiply(){  
              someValue=3;  
              SimpleStorage aaa=SimpleStorage(DeployedAddresses.SimpleStorage());  
              value = aaa.multiply(someValue);  
              uint expected = 21;  
              Assert.equal(value, expected, "should 3*7=21");  
        }  
    }   

运行测试:

首先启动testrpc :

其次
执行truffle migrate,如果以前有编译过别的乱七八糟的合约,怕环境出问题,可以使用truffle migrate –reset:
这里写图片描述

执行 truffle test,进行合约的测试,可以具体指明测试哪个,如不具体指明,则测试所有的:
这里写图片描述
此为通过测试.

可以试着将TestSimpleStorage.sol中的代码稍作替换:

    uint expected = 21; -->uint expected = 22;  

这里写图片描述

此处编译未通过.

http://blog.csdn.net/jerry81333/article/details/70837681
https://juejin.im/post/58f97521b123db41195481f3?utm_source=tuicool&utm_medium=referral
http://truffleframework.com/docs/getting_started/solidity-tests

猜你喜欢

转载自blog.csdn.net/niekai01/article/details/71437255
今日推荐