首页 > 解决方案 > “npm run test”期间出错:“未定义”的参数数量无效。得到 1 预期 0

问题描述

我在进行 npm 运行测试时收到此错误消息。我正在为智能合约部署测试彩票应用程序运行断言测试。我已经声明了所需的库和提供程序,并在其他测试中尝试了类似的代码。在我尝试运行此测试以确保部署正常运行之前,它们运行良好,但失败了。

这是我的代码:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { interface, bytecode } = require('../compile');

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', () => {
  it('deploys a contract', () => {
    assert.ok(lottery.options.address);
  });
});

我得到这个结果:

$ npm run test

> Lottery@1.0.0 test /Volumes/Seagate Backup Plus Drive/Development/EthereumSolidity/lottery
> mocha



  Inbox
    1) "before each" hook for "deploys a contract"


  0 passing (137ms)
  1 failing

  1) "before each" hook for "deploys a contract":
     Error: Invalid number of parameters for "undefined". Got 1 expected 0!
      at Object.InvalidNumberOfParams (node_modules/web3-core-helpers/src/errors.js:32:16)
      at Object._createTxObject (node_modules/web3-eth-contract/src/index.js:813:22)
      at Contract.deploy (node_modules/web3-eth-contract/src/index.js:611:33)
      at Context.beforeEach (test/inbox.test copy 2.js:16:6)
      at process.internalTickCallback (internal/process/next_tick.js:77:7)



npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Lottery@1.0.0 test: `mocha`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the Lottery@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我不确定我在断言期间做错了什么。感谢任何可以提供帮助的想法,谢谢。

标签: javascriptnode.jstestingassertionsmartcontracts

解决方案


我解决了这个问题。我发现如果你在同一个测试文件夹中有旧的测试文件,它会在断言期间导致这个错误。

如果您有一个名为“Test”的测试文件夹,并且您有多个测试文件,例如:

测试1.js,测试2.js,测试3.js

节点测试会出现问题并生成此错误。我删除了所有旧的测试文件,只保留了最新的,例如:

test3.js

错误消失了,现在所有断言都已正确测试。


推荐阅读