如何为 javascript/truffle 中的每个测试创建新的 ethereum/solidity 合同
how to create new ethereum/solidity contract for each test in javascript/truffle
背景
我在 Solidity language. In order to test things, I can run a local node using 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, but truffle 为您定义 artifacts
与智能合约的 JavaScript 连接。
truffle contract
函数 is almost the same 作为 Mocha 的 describe
函数,有一个我不明白的小变化!我假设 contract
每次都会让我的合同更新。它没有。也许我可以使用 new Round()
之类的东西来代替 Round.deployed()
,但我只是不知道该怎么做。
该解决方案没有使用松露。
请注意 .new
和 .deployed
不一样。看看我发现了什么 here.
按照这个例子应该可以解决你的问题:
// 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
实际上会使用您之前部署的合约,即当您使用 truffle migrate
命令时。
在单元测试的上下文中,最好使用.new
这样你总是会从新合同开始。
背景
我在 Solidity language. In order to test things, I can run a local node using 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, but truffle 为您定义 artifacts
与智能合约的 JavaScript 连接。
truffle contract
函数 is almost the same 作为 Mocha 的 describe
函数,有一个我不明白的小变化!我假设 contract
每次都会让我的合同更新。它没有。也许我可以使用 new Round()
之类的东西来代替 Round.deployed()
,但我只是不知道该怎么做。
该解决方案没有使用松露。
请注意 .new
和 .deployed
不一样。看看我发现了什么 here.
按照这个例子应该可以解决你的问题:
// 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
实际上会使用您之前部署的合约,即当您使用 truffle migrate
命令时。
在单元测试的上下文中,最好使用.new
这样你总是会从新合同开始。