DeployedAddresses + Error:处理事务时的VM异常:还原

时间:2018-03-30 19:05:50

标签: ethereum truffle

我有一份合同如下 -

文件名:dummycontrat.sol

val dayPrices = Transformations.switchMap(DoubleTrigger(code, nbDays)) {
    dbManager.getDayPriceData(it.first, it.second)
}

我有以下测试文件 -

FileName:test / TestDummyContract.sol

pragma solidity ^0.4.17;

contract DummyContract {

    function fetchRandomNumber() public pure returns(uint) {
        uint res = 10;
        return res;
    }

}

我运行命令 -

pragma solidity ^0.4.17;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/dummycontract.sol";

    contract TestDummyContract {

        function testRandomNumberNew() public {
            DummyContract dummyContract = new DummyContract();
            uint randomNumber = dummyContract.fetchRandomNumber();
            Assert.equal(randomNumber, 10, "Number is not 10");
        }

        function testRandomNumberDeployed() public {
            DummyContract dummyContract = DummyContract(DeployedAddresses.DummyContract());
            uint randomNumber = dummyContract.fetchRandomNumber();
            Assert.equal(randomNumber, 10, "Number is not 10");
        }

    }

第一次测试通过而第二次测试错误。 truffle compile && truffle migrate --reset --network dev && truffle test --network dev test/TestDummyContract.sol 命令的日志如下 -

truffle test

任何人都可以解释这里的问题是什么吗?我在OSX上为网络运行Ganache工具。

1 个答案:

答案 0 :(得分:1)

DeployedAddresses.DummyContract()失败,因为只有包含在Truffle部署配置中的合同可供DeployedAddresses使用。来自Project System Documentation

  

您已部署的合同的地址(即作为迁移的一部分部署的合同)可通过truffle / DeployedAddresses.sol库获得。这是由Truffle提供的,并在每个套件运行之前重新编译和重新链接,以提供Truffle的洁净室环境测试。该库以以下形式提供所有已部署合同的功能:

     

DeployedAddresses.<contract name>();

要解决您的问题,您需要在/ migrations下创建部署配置(或者如果您有,则添加到现有配置)。例如:

<强> 2_deploy_contracts.js

var DummyContract = artifacts.require("dummycontract");

module.exports = function(deployer) {
  deployer.deploy(DummyContract);
};

添加该配置,重新运行truffle migrate --reset,然后您的测试就可以了。

相关问题