Solidity 函数未更新文件中的值
Solidity function is not updating the value in the file
这是我的代码:
pragma solidity >=0.4.17;
contract Inbox {
string public message;
constructor(string memory _initMessage) public {
message = _initMessage;
}
function setMessage(string memory _newMessage) public {
message = _newMessage;
}
}
这是我用来编译 solidity 代码的 compile.js 代码:
const path = require("path");
const fs = require("fs");
const solc = require("solc");
const inboxPath = path.resolve(__dirname, "contracts", "Inbox.sol");
const source = fs.readFileSync(inboxPath, "utf8");
module.exports = solc.compile(source, 1).contracts[":Inbox"];
这是我的 inbox.test.js 文件,我在其中编写了测试。
const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const provider = ganache.provider();
const web3 = new Web3(provider);
const { interface, bytecode } = require("../compile");
//global variables
let accounts;
let inbox;
beforeEach(async () => {
// get the list of all accounts
accounts = await web3.eth.getAccounts();
// use one of the 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" });
inbox.setProvider(provider);
});
describe("Inbox", () => {
it("deploys a contract", () => {
assert.ok("inbox.option.address");
});
it("set initial value", async () => {
const messsage = await inbox.methods.message().call();
assert.equal(messsage, "Hi there!");
});
it("can set new value", async () => {
await inbox.methods.setMessage("bye").call({ from: accounts[0] });
const mess = await inbox.methods.message().call();
assert.equal(mess, "bye");
});
});
问题:当我执行 'npm run test' 命令时,我的前 2 个测试成功通过,但第 3 个测试失败,显示 'Hi there!' 不等于 "bye".
~/Documents/Inbox$ npm run test
> inbox@1.0.0 test /home/sahil/Documents/Inbox
> mocha
Inbox
(node:19409) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added to [l]. Use emitter.setMaxListeners() to increase limit
✓ deploys a contract
✓ set initial value
1) can set new value
2 passing (511ms)
1 failing
1) Inbox
can set new value:
AssertionError [ERR_ASSERTION]: 'Hi there!' == 'bye'
+ expected - actual
-Hi there!
+bye
at Context.<anonymous> (test/Inbox.test.js:38:12)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! inbox@1.0.0 test: `mocha`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the inbox@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/sahil/.npm/_logs/2020-03-30T05_58_56_753Z-debug.log
我检查了所有内容,但我不知道为什么 'setMessage' 函数没有更新我在 solidity 文件中的消息值。请帮忙
您正在使用 .call()
调用方法,而不是使用 .send()
进行交易。这应该可以解决您的失败测试。
await inbox.methods.setMessage("bye").send({ from: accounts[0] });
这是我的代码:
pragma solidity >=0.4.17;
contract Inbox {
string public message;
constructor(string memory _initMessage) public {
message = _initMessage;
}
function setMessage(string memory _newMessage) public {
message = _newMessage;
}
}
这是我用来编译 solidity 代码的 compile.js 代码:
const path = require("path");
const fs = require("fs");
const solc = require("solc");
const inboxPath = path.resolve(__dirname, "contracts", "Inbox.sol");
const source = fs.readFileSync(inboxPath, "utf8");
module.exports = solc.compile(source, 1).contracts[":Inbox"];
这是我的 inbox.test.js 文件,我在其中编写了测试。
const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const provider = ganache.provider();
const web3 = new Web3(provider);
const { interface, bytecode } = require("../compile");
//global variables
let accounts;
let inbox;
beforeEach(async () => {
// get the list of all accounts
accounts = await web3.eth.getAccounts();
// use one of the 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" });
inbox.setProvider(provider);
});
describe("Inbox", () => {
it("deploys a contract", () => {
assert.ok("inbox.option.address");
});
it("set initial value", async () => {
const messsage = await inbox.methods.message().call();
assert.equal(messsage, "Hi there!");
});
it("can set new value", async () => {
await inbox.methods.setMessage("bye").call({ from: accounts[0] });
const mess = await inbox.methods.message().call();
assert.equal(mess, "bye");
});
});
问题:当我执行 'npm run test' 命令时,我的前 2 个测试成功通过,但第 3 个测试失败,显示 'Hi there!' 不等于 "bye".
~/Documents/Inbox$ npm run test
> inbox@1.0.0 test /home/sahil/Documents/Inbox
> mocha
Inbox
(node:19409) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added to [l]. Use emitter.setMaxListeners() to increase limit
✓ deploys a contract
✓ set initial value
1) can set new value
2 passing (511ms)
1 failing
1) Inbox
can set new value:
AssertionError [ERR_ASSERTION]: 'Hi there!' == 'bye'
+ expected - actual
-Hi there!
+bye
at Context.<anonymous> (test/Inbox.test.js:38:12)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! inbox@1.0.0 test: `mocha`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the inbox@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/sahil/.npm/_logs/2020-03-30T05_58_56_753Z-debug.log
我检查了所有内容,但我不知道为什么 'setMessage' 函数没有更新我在 solidity 文件中的消息值。请帮忙
您正在使用 .call()
调用方法,而不是使用 .send()
进行交易。这应该可以解决您的失败测试。
await inbox.methods.setMessage("bye").send({ from: accounts[0] });