简体   繁体   中英

I'm having a problem with my test, when I run them I'm getting error

I'm having a problem with my test, when I run them I'm getting this error:

Contract: Electionn √ initializes with two candidates 1) it initializes the candidates with the correct values

No events were emitted

1 passing (89ms) 1 failing

1) Contract: Electionn it initializes the candidates with the correct values: ReferenceError: Election is not defined at Context. (test\\election.js:16:5) at C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\truffle-core\\lib\\testing\\testrunner.js:135:1 at C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\~\\web3\\lib\\web3\\property.js:119:1 at C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\~\\web3\\lib\\web3\\requestmanager.js:89:1 at C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\truffle-provider\\wrapper.js:134:1 at XMLHttpRequest.request.onreadystatechange (C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\~\\web3\\lib\\web3\\httpprovider.js:128:1) at XMLHttpRequestEventTarget.dispatchEvent (C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\~\\xhr2\\lib\\xhr2.js:64:1) at XMLHttpRequest._setReadyState (C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\~\\xhr2\\lib\\xhr2.js:354:1) at XMLHttpRequest._onHttpResponseEnd (C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack :\\~\\xhr2\\lib\\xhr2.js:509:1) at IncomingMessage. (C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\~\\xhr2\\lib\\xhr2.js:469:1) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9)

and my test file is :

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");
    });
 });
});

I don't know how to solve it, thank you in advance.

You don't need to reference the deployed version of the contract for testing purposes...

Try initializing your contract instance before each test, and use a global variable to reference it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM