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');
});
});
运行上述代码后,我不断收到以下错误
inbox@1.0.0测试C:\ Users \ user \ Documents \ inbox
摩卡咖啡
收件箱 1)“先于”钩子用于“部署合同”
0通过(98ms) 1个失败
1)“在每个之前”钩子用于“部署合同”: SyntaxError:JSON中位置0处的意外令牌u 在JSON.parse() 在Context.beforeEach(test \ inbox.test.js:16:44)
npm错误!代码ELIFECYCLE
npm ERR! errno 1
npm ERR! inbox@1.0.0测试:mocha
npm ERR!退出状态1
npm ERR!
npm ERR! inbox@1.0.0测试脚本失败。
npm ERR! npm可能不是问题。上面可能还有其他日志记录输出。
npm错误!可以在以下位置找到此运行的完整日志: npm ERR! C:\ Users \ user \ AppData \ Roaming \ npm-cache_logs \ 2018-07-03T13_17_54_895Z-debug.log
C:\ Users \ user \ Documents \ inbox \ test>
答案 0 :(得分:2)
当我在compile.js文件中将编码从'UTF-8'更改为'utf8'时,它就起作用了。
我的compile.js文件看起来像这样
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'];
答案 1 :(得分:0)
大家好,我之前也遇到过这个问题。 无需更改complie.js文件。 只是我们需要在声明部分添加点缀
像您的情况一样:
而不是写这个
const {interface, bytecode} = require("../compile")
我们可以这样写
const {interface} = require("../compile")
const {bytecode} = require("../compile")
在这种情况下,我们同时获取接口和字节码值,这些值将从compile.js文件导出。