如何测试另一个已部署合同生成的合同?

时间:2019-05-03 08:16:25

标签: ethereum solidity truffle

当前,我想测试一个名为B的合约,该合约是通过使用松露从另一个合约Registry生成的。例如:

合同Registry

pragma solidity >=0.4.17;
import "./B.sol";
contract Registry is Owned {

    function createContract(
        address userAddress) 
        public
        returns(
            bool res
        )
    {
        // create a contract B which the owner is the user.
        B b = new B(userAddress);

        return true;
    }

合同B

pragma solidity >=0.4.17;
contract B {
    address private owner;

    constructor (
        address _owner
    ) 
        public
    {
        owner = _owner;
    }

    function logData(
        bytes32 data,
        uint    timestamp
    )
        public
        returns(bool res)
    {
        ...
    }

    ...

松露/测试中的测试脚本B.js

const Registry = artifacts.require('./Registry.sol');
const B = artifacts.require('./B.sol');

const truffleAssert = require('truffle-assertions');
const assert = require('assert');

contract('Registry', (accounts) => {
  let registry;
  let B;

  beforeEach(async () => {
      // create a reportSummary contract from registry contract. 
      registry = await Registry.deployed();

      // How can I retrieve the contract B's instance?
  });

由于我是新手,因此无法找到任何方法来测试功能logData或依赖于合同B的合同Registry中的其他功能。有什么合适的方法还是我应该更改设计的合同?谢谢。

1 个答案:

答案 0 :(得分:0)

您也可以查看我的答案,它也提供了有关从导入的合同访问功能的示例

Solidity: Can a Parent contract see data updates from a Child contract?

要通过web3访问该功能,只需使用Registry Contract的实例,即可调用Contract B的功能