[英]How to manually create the instance of the contract in truffle
说我有2个像这样的合同
A.sol
import './B.sol';
contract A {
event BCreated(address addressOfB);
function createB(){
B b = new B();
BCreated(b);
}
}
B.sol
contract B {
uint8 value = 5;
function getValue() constant returns(uint8){
return value;
}
}
我正在尝试为这些合同编写测试用例。 我可以使用迁移文件部署合同A,并获取它的实例。
但是在使用函数createB()创建合同之后,我不确定如何获得合同B的实例
好的,我可以在调用函数createB()后得到事件中合约B的地址,但不确定实例。
对于这个例子,你可以说我可以单独测试合同B,因为它没有做太多。 但在实际情况中,我需要使用来自事件的地址创建实例。
这里是松露测试文件的一点点js代码在这里我有B的地址
var A = artifacts.require("./A.sol");
contract('A', (accounts) => {
it("Value should be 5", async () => {
let instanceOfA = await A.deployed()
let resultTx = await instanceOfA.createB({ from: accounts[0] });
console.log("Address of B: " + resultTx.logs[0].args.addressOfB);
/**
* How do I create the instance of B now?
*/
})
})
你可以尝试这样做
var A = artifacts.require("./A.sol");
var B = artifacts.require("./B.sol");
contract('A', (accounts) => {
it("Value should be 5", async () => {
let instanceOfA = await A.deployed()
let resultTx = await instanceOfA.createB({ from: accounts[0] });
console.log("Address of B: " + resultTx.logs[0].args.addressOfB);
let instanceOfB = await B.at(resultTx.logs[0].args.addressOfB);
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.