Ethereum Ошибка терминала Mocha - «Ошибка: не удается найти модуль 'randomhex'» - PullRequest
0 голосов
/ 05 августа 2020

Я сейчас прохожу этот курс udemy по разработке сетей на Ethereum. Курс проводился два года go, поэтому мне приходилось использовать старые версии всего, чтобы пройти. Мой код работал нормально при выполнении «npm run test», но после добавления оператора в строку 29 (оператор assert.ok) я начал получать эту ошибку. Я не понимаю, в чем ошибка терминала. Помощь?

inbox.test. js:

//inbox.test.js
const assert = require('assert'); // used for ganache assertion
const ganache = require('ganache-cli'); // local ethereum testing netwrok 
const Web3 = require('web3'); // Web3 is a constructor function (that's why it is capatalized)
const { interface, bytecode } = require('../compile'); // descructors - going up the directory tree 

 // creating an instance of Web3
const provider = ganache.provider();
const web3 = new Web3(provider);

let accounts; // to be accessed globally 
let inbox; // holds the deployed contract 

beforeEach( async () => {
// Get a list of all accounts 
accounts = await web3.eth.getAccounts(); //eth is a module that has a lot of functions used for development 
   
// Use one of the accounts to deploy the contract 
inbox = await new web3.eth.Contract (JSON.parse(interface)) // instance of a contract
.deploy({data: bytecode, arguments: ['Hi There!'] })
.send ({from: accounts[0], gas:'1000000'}); 

inbox.setProvider(provider); 

});

describe('Inbox', ()=> {
    it('deployes a contract', () => {
     assert.ok(inbox.options.address); // to check if the contract is successfuly depolyed 
    });

    it('has a default message', async () => {
      const message = await inbox.methods.message().call();
      assert.equal(message, 'Hi there!');
    });
    
    it('can change the message', async () => {
      await inbox.methods.setMessage('bye').send( {from: accounts[0]} );const message = await inbox.methods.message().call();
      assert.equal(message, 'bye'); 
    });


});

Ошибка терминала:

Терминал Ошибка 1

Ошибка терминала 2

Пакет. json:

{
  "name": "inbox",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "Eiman",
  "license": "ISC",
  "dependencies": {
    "ganache-cli": "^6.9.1",
    "mocha": "^8.1.0",
    "solc": "^0.4.17",
    "web3": "^1.0.0-beta.26"
  }
}

компилировать. js:

// Modules 
const path = require ('path'); // module used to help build a path from compile.js to inbox.sol - guaranteed to get compatibility with OS used  
const fs = require ('fs');
const solc = require ('solc'); 

const inboxPath = path.resolve(__dirname, 'contracts', 'inbox.sol' );
const source = fs.readFileSync(inboxPath, 'utf8'); // to read the content of the inbox.sol file
module.exports = solc.compile(source, 1).contracts[':Inbox']; // compile statement 


inbox.sol:

pragma solidity ^0.4.17;

contract Inbox {

    string public message;



    function Inbox (string initalMessage) public {
        message = initalMessage;
    }




    function setMessage(string newMessage) public {
        message = newMessage;
    }
}

1 Ответ

0 голосов
/ 05 августа 2020

Ваш код работает без проблем на моем Ma c (строки на самом деле разные):

Inbox
    ✓ deployes a contract
    1) has a default message
    ✓ can change the message (74ms)


  2 passing (405ms)
  1 failing

  1) Inbox
       has a default message:

      AssertionError [ERR_ASSERTION]: 'Hi There!' == 'Hi there!'
      + expected - actual

      -Hi There!
      +Hi there! 

Похоже, проблема с вашими node_modules, попробуйте fre sh install.

...