Ethereum 测试坚固性时遇到的问题

Ethereum 测试坚固性时遇到的问题,ethereum,solidity,Ethereum,Solidity,在运行上述代码之后,我不断得到以下错误 inbox@1.0.0测试C:\Users\user\Documents\inbox 摩卡咖啡 收件箱 1) 在每个“钩子”部署契约之前 0通过(98毫秒) 1失败 1) 在每个“钩子”部署契约之前: SyntaxError:JSON中位于位置0的意外标记u 在JSON.parse()处 在Context.beforeach(test\inbox.test.js:16:44) 在 npm错误!代码失效循环 npm错误!错误1 npm错误!inbox@1.0

在运行上述代码之后,我不断得到以下错误

inbox@1.0.0测试C:\Users\user\Documents\inbox

摩卡咖啡

收件箱 1) 在每个“钩子”部署契约之前

0通过(98毫秒) 1失败

1) 在每个“钩子”部署契约之前: SyntaxError:JSON中位于位置0的意外标记u 在JSON.parse()处 在Context.beforeach(test\inbox.test.js:16:44) 在

npm错误!代码失效循环 npm错误!错误1 npm错误!inbox@1.0.0测试:
mocha
npm错误!退出状态1 npm错误! npm错误!失败inbox@1.0.0测试脚本。 npm错误!这可能不是npm的问题。上面可能还有其他日志输出

npm错误!此运行的完整日志可在以下位置找到: npm错误!C:\Users\user\AppData\Roaming\npm-cache\u logs\2018-07-03T13\u 17\u 54\u 895Z-debug.log


C:\Users\user\Documents\inbox\test>

当我在compile.js文件中将编码从“UTF-8”更改为“utf8”时,它起作用了

我的compile.js文件如下所示

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 accounts;
let inbox;

beforeEach(async () => {
  // Get a list of all accounts
  accounts = await web3.eth.getAccounts();

  // Use one of those accounts to deploy
  // the contract
  inbox = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({
      data: bytecode,
      arguments: ['Hi there!']
    })
    .send({ from: accounts[0], gas: '1000000' });
});

describe('Inbox', () => {
    it('deploys a contract', () => {
    assert.ok(inbox.options.address);
  });

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

当我在compile.js文件中将编码从“UTF-8”更改为“utf8”时,它起了作用

我的compile.js文件如下所示

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 accounts;
let inbox;

beforeEach(async () => {
  // Get a list of all accounts
  accounts = await web3.eth.getAccounts();

  // Use one of those accounts to deploy
  // the contract
  inbox = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({
      data: bytecode,
      arguments: ['Hi there!']
    })
    .send({ from: accounts[0], gas: '1000000' });
});

describe('Inbox', () => {
    it('deploys a contract', () => {
    assert.ok(inbox.options.address);
  });

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

嗨,伙计们,我以前也面临过这个问题。 complie.js文件中没有任何需要更改的内容。 我们只需要在声明部分修改一下

比如你的情况:

而不是写这个
const{interface,bytecode}=require(../compile”)

我们可以这样写

const path = require('path'); //for crossplatform
const fs = require('fs'); //file system
const solc = require('solc');

const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8'); //change to utf8 to UTF-8

module.exports = solc.compile(source, 1).contracts[':Inbox'];

在这种情况下,我们得到了接口和字节码值,它们从compile.js文件导出。

大家好,我以前也遇到过这个问题。 complie.js文件中没有任何需要更改的内容。 我们只需要在声明部分修改一下

比如你的情况:

而不是写这个
const{interface,bytecode}=require(../compile”)

我们可以这样写

const path = require('path'); //for crossplatform
const fs = require('fs'); //file system
const solc = require('solc');

const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8'); //change to utf8 to UTF-8

module.exports = solc.compile(source, 1).contracts[':Inbox'];

在本例中,我们得到了从compile.js文件导出的接口和字节码值。

我发现了错误。它在编译文件中,而不是测试文件中。我认为ABI接口有问题。您可以检查从compile导入的接口变量吗?我发现了我的错误。它在编译文件中,而不是测试文件中。我认为ABI接口有问题。您可以检查从compile导入的接口变量吗?