Truffle
如何為松露中部署的合約配置預設參數?
這是我如何使用帶建構子參數部署的後續問題
為了使用 mocha 進行測試,我想配置我的預設部署合約,以便使用正確的參數對其進行實例化。有沒有辦法配置松露/布丁,以便始終使用某個參數值部署合約?
但是,您可以通過呼叫在測試中創建一個新實例
Contract.new(params).then(...)
(其中 ‘Contract’ 是特定合約類名稱的名稱,如 Ballot.new() 或 MetaCoin.new() )。您可以在測試的 before() 函式中設置一個合約實例,然後使用該實例進行測試(您應該在 before() 呼叫中設置一些 contract() 範圍變數,然後在 it() 測試中測試它們) .
IE:
var expect = require('chai').expect; var log = console.log; function clearBytes32(str){ return str.replace(/\u0000/g, ''); } function toInteger( bigNumber){ return parseInt(bigNumber.toString() ); } contract("Ballot" , function(accounts){ var ballot = null; describe("new()" , function(){ var chairperson = null; var proposalsCount = null; var firstProposal = null; before(function(done){ Ballot.new(["hard fork" , "soft fork" , "do nothing"]) .catch(log) .then(function(contract){ ballot = contract; }) .catch(log) .then( function(){ return ballot.chairperson(); }) .catch(log) .then(function(response){ chairperson = response; return ballot.getProposal(0); }) .catch(log}) .then(function(response){ firstProposal = response; return ballot.proposalsCount(); }) .catch(log) .then(function(response){ proposalsCount = response; }) .then(done); }); it("ballot should be set" , function(){ expect(ballot).to.not.be.null; } ); it("ballot chairperson should be coinbase" , function(){ expect(chairperson).to.be.equal(accounts[0]); }); it("ballot first proposal should be set" , function(){ var ascii = web3.toAscii(firstProposal[0]); expect(clearBytes32(ascii)).to.be.equal("hard fork"); }); it("ballot proposals count should be 3" , function(){ expect( toInteger(proposalsCount) ).to.be.equal(3); }); describe("vote()" , function(){ //ballot instance is available here. }); });