我的测试有问题,运行它们时出现错误

时间:2018-11-06 09:30:16

标签: javascript testing ethereum solidity truffle

我的测试有问题,运行它们时出现此错误:

  

合同:选举       √用两个候选者初始化       1)使用正确的值初始化候选对象

     
    

没有事件发出

  
     

1个通过(89ms)1个失败

     

1)合同:Electionn          它使用正确的值初始化候选者:        ReferenceError:未定义选举         在上下文。 (test \ election.js:16:5)         在C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\ packages \ truffle-core \ lib \ testing \ testrunner.js:135:1         在C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ web3 \ lib \ web3 \ property.js:119:1         在C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ web3 \ lib \ web3 \ requestmanager.js:89:1         在C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\ packages \ truffle-provider \ wrapper.js:134:1         在XMLHttpRequest.request.onreadystatechange处(C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ web3 \ lib \ web3 \ httpprovider.js:128:1)         在XMLHttpRequestEventTarget.dispatchEvent(C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ xhr2 \ lib \ xhr2.js:64:1)         在XMLHttpRequest._setReadyState(C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ xhr2 \ lib \ xhr2.js:354:1)         在XMLHttpRequest._onHttpResponseEnd(C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ xhr2 \ lib \ xhr2.js:509:1)         在IncomingMessage。 (C:\ Users \ HP \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ xhr2 \ lib \ xhr2.js:469:1)         在endReadableNT(_stream_visible.js:1064:12)         在_combinedTickCallback(内部/进程/next_tick.js:138:11)         在process._tickCallback(internal / process / next_tick.js:180:9)

我的测试文件是:

var Electionn = artifacts.require("./Electionn.sol");

contract("Electionn", function(accounts) {
  var electionInstance;

  it("initializes with two candidates", function() {
    return Electionn.deployed().then(function(instance) {
      return instance.candidatesCount();
    }).then(function(count) {
      assert.equal(count, 2);
    });
  });

  it("it initializes the candidates with the correct values", function() {
    return Election.deployed().then(function(instance) {
      electionInstance = instance;
      return electionInstance.candidates(1);
    }).then(function(candidate) {
      assert.equal(candidate[0], 1, "contains the correct id");
      assert.equal(candidate[1], "Candidate 1", "contains the correct name");
      assert.equal(candidate[2], 0, "contains the correct votes count");
      return electionInstance.candidates(2);
    }).then(function(candidate) {
      assert.equal(candidate[0], 2, "contains the correct id");
      assert.equal(candidate[1], "Candidate 2", "contains the correct name");
      assert.equal(candidate[2], 0, "contains the correct votes count");
    });
 });
});

我不知道该怎么解决,谢谢。

1 个答案:

答案 0 :(得分:0)

出于测试目的,您无需引用合同的部署版本...

尝试在每次测试之前初始化合约实例,并使用全局变量对其进行引用。

var electionnInstance;

beforeEach(function() {
    return Electionn.new()
    .then(function(instance) {
        electionnInstance = instance;
    });
});

it("..."