我不知道为什么在终端中键入“ npm run test”时会出现此错误。看起来它是从lottery.test.js文件生成的。我仔细检查了语法,似乎我从编译文件中正确导出了模块。但是,我仍然在测试文件中出现错误。对于任何想知道的人,统一文件的名称都是Lottery.sol。
Lottery.test.js:
// Modules
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)
// creating an instance of Web3
const provider = ganache.provider();
const web3 = new Web3(provider);
const { interface, bytecode } = require('../compile'); // descructors - going up the directory tree
let lottery;
let accounts;
beforeEach( async () => {
accounts = await web3.eth.getAccounts();
lottery = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode })
.send({ from: accounts[0], gas: '1000000' });
});
describe('Lottery Contract', () => {
// General Test - Contact has been deployed
it('deployes a contract', ()=> {
assert.ok(lottery.options.address);
});
// Test One - Allows one account to be enter
it('allows one account to enter', async () => {
await lottery.methods.enter().send({
from: accounts[0],
value: web3.utils.toWei('0.02', 'ether') // converts value from ether to wei since value is measured in wei
});
const players = await lottery.methods.getPlayers().call({from: accounts[0]});
assert.equal(accounts[0], players[0]);
assert.equal(1, players.length);
});
// Test Two - Allows more than one account to be enter
it('allows multiple accounts to enter', async () => {
await lottery.methods.enter().send({
from: accounts[0],
value: web3.utils.toWei('0.02', 'ether') // converts value from ether to wei since value is measured in wei
});
await lottery.methods.enter().send({
from: accounts[1],
value: web3.utils.toWei('0.02', 'ether') // converts value from ether to wei since value is measured in wei
});
await lottery.methods.enter().send({
from: accounts[2],
value: web3.utils.toWei('0.02', 'ether') // converts value from ether to wei since value is measured in wei
});
const players = await lottery.methods.getPlayers().call({from: accounts[0]});
assert.equal(accounts[0], players[0]);
assert.equal(accounts[1], players[1]);
assert.equal(accounts[2], players[2]);
assert.equal(1, players.length);
});
// Test Three - Only accounts that send more than 1 Ether can enter
// Done by sending an entery fee LESS than what is expected and anticipating the error
// Using a Try-Catch structure
it('requires a minimum amount of ether to enter', async () => {
try { // trying to push an error to happen
await lottery.methods.enter().send({
from: accounts[0],
value: 0
});
assert(false); // pushing the function to generate an error
}
catch(err){ // triggered when an error is triggered
assert(err);
}
});
// Test Four - Only the manager can pick the winner
// By letting someone else BESIDE the manager choose the winner causing an error
it ('Only manager can call pickWinner', async () => {
try {
await lottery.methods.pickWinner().send({
from: accounts[1] // someone else - not the manager
});
assert(false);
} catch(err){
assert(err);
}
});
// Test Five - Resets the lottery when done
it('Sends money to the winner and resets the players array', async () => {
// 1. Entering the lottery means the player sends an amount of Ether (2 Ether here)
await lottery.methods.enter().send({
from: accounts[0],
value: web3.untils.toWei('2', 'ether')
});
// 2. Inital balance has to be less by 2 Ether
const initialBalance = await web3.eth.getBalance(accounts[0]); // amount of Ether in units of wei in an account
// 3. After picking the winner, the amountd are reset
await lottery.methods.pickWinner().send({from: accounts[0]});
// 4. Final balance should be more by 2 Ehter (normal before entering)
const finalBalance = await web3.eth.getBalance(accounts[0]);
// 5. Difference has to be less than 2 Ether - its not exactly 2 because we have to pay for gas
const difference = finalBalance - initialBalance;
assert(difference > web3.untils.toWei('1.8', 'ether'));
});
});
compile.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 lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(lotteryPath, 'utf8'); // to read the content of the inbox.sol file
module.exports = solc.compile(source, 1).contracts[':Lottery']; // compile statement
答案 0 :(得分:1)
因为我无法评论 ngambinos 的帖子。这在 2021 年也对我有用。 (感谢)ngambino0192:
问题可能在于 solc 编译器的版本控制。将版本的依赖项转换为更新的版本可能会解决您的问题。
尝试以下操作:
然后你可以运行你的测试