如何为javascript / truffle中的每个测试创建新的以太坊/实体合约

时间:2018-10-07 16:19:55

标签: javascript testing solidity smartcontracts truffle

背景

我用Solidity语言编写了一个以太坊智能合约。为了进行测试,我可以使用Ganache运行本地节点,并使用truffle migrate在其上部署合同。

要求

我想使用JavaScript测试我的合同。我想为每个测试创建合同的 new 实例。

我尝试了什么

我在项目中创建了一个测试文件tests/test.js

const expect = require('chai').expect

const Round = artifacts.require('Round')


contract('pledgersLength1', async function(accounts) {
    it('1 pledger', async function() {
        let r = await Round.deployed()
        await r.pledge(5)
        let len = (await r.pledgersLength()).toNumber()
        expect(len).to.equal(1)
    })
})
contract('pledgersLength2', async function(accounts) {
    it('2 pledgers', async function() {
        let r = await Round.deployed()
        await r.pledge(5)
        await r.pledge(6)
        let len = (await r.pledgersLength()).toNumber()
        expect(len).to.equal(2)
    })
})

我用truffle test运行它。它基本上是Mocha,但是truffle通过与智能合约的JavaScript连接为您定义了artifacts

松露contract函数is almost the same就像Mocha的describe函数一样,有一点我不理解的变化!我假设contract将使我的合同每次都更新。没有。也许我可以使用类似new Round()之类的东西来代替Round.deployed(),但我只是不知道怎么做。

该解决方案没有使用松露。

1 个答案:

答案 0 :(得分:2)

请注意,getString() protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (scanResult != null) { Log.w(TAG, "onActivityResult() QR content -> " + scanResult.getContents()); switch (requestCode) { case USER_INFO_REQUEST: do.Something(scanResult.getContents()); break; case ORG_USER_INFO_REQUEST: do.Something(scanResult.getContents()); break; default: Log.w(TAG,"onActivityResult() not yet implemented"); break; } } else { Log.e(TAG, "onActivityResult() empty QR response"); Log.w(TAG, "onActivityResult() result code -> " + resultCode); Log.w(TAG, "onActivityResult() request code -> " + requestCode); Log.w(TAG, "onActivityResult() data -> " +data); try { Bundle bundle = data.getExtras(); switch (requestCode){ case USER_INFO_REQUEST: do.Something(bundle.getString(AppConst.KEY_BUNDLE_SCAN_RESULT)); break; case ORG_USER_INFO_REQUEST: do.Something(bundle.getString(AppConst.KEY_BUNDLE_SCAN_RESULT)); break; default: Log.w(TAG,"onActivityResult() not yet implemented"); break; } }catch (NullPointerException npEx){ Log.e(TAG,"onActivityResult() NPEX -> "+npEx.toString()); }catch (Exception ex){ Log.e(TAG,"onActivityResult() -> "+ex.toString()); } super.onActivityResult(requestCode, resultCode, data); } } 是不同的。看看我发现的here

按照以下示例操作,它将解决您的问题:

.new

.deployed关键字将在新的上下文中部署合同的实例。

但是,// Path of this file: ./test/SimpleStorage.js var simpleStorage = artifacts.require("./SimpleStorage.sol"); contract('SimpleStorage', function(accounts) { var contract_instance; before(async function() { contract_instance = await simpleStorage.new(); }); it("owner is the first account", async function(){ var owner = await contract_instance.owner.call(); expect(owner).to.equal(accounts[0]); }); }); 实际上将使用您先前部署的合同,即,在使用.new命令时。

在单元测试的上下文中,最好使用.deployed,以便您始终从新合同开始。