[英]truffle & solidity cant access to a function in test
after I run command truffle test I got this error在我运行命令 truffle test 后,我得到了这个错误
TypeError: _character.getName is not a function类型错误:_character.getName 不是 function
my test file我的测试文件
let Game = artifacts.require("./Game.sol");
let Character = artifacts.require("./Character.sol");
const PROVIDED_NAME = "TEST";
contract("Game", (accounts) => {
let creatorAccount = accounts[0];
it("should create a character with the name provided", () => {
let _game;
let _character;
return Game.deployed()
.then(instance => {
_game = instance;
return _game.createCharacter(PROVIDED_NAME, { from: creatorAccount });
})
.then(result => {
return _game.getCharacter();
})
.then(character => {
_character = Character.at(character);
return _character.getName();
})
.then(name => {
assert.equal(name, PROVIDED_NAME, "Failed to create a character with the provided name");
});
});
});
and this my Character.sol file这是我的 Character.sol 文件
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
contract Character {
string private _name;
constructor(string memory name) public {
_name = name;
}
function getName() public view returns (string memory) {
return _name;
}
}
I'm using我在用着
Truffle v5.5.20 (core: 5.5.20) Ganache v7.2.0 Solidity - 0.8.15 (solc-js) Node v16.15.1 Web3.js v1.7.4 Truffle v5.5.20 (core: 5.5.20) Ganache v7.2.0 Solidity - 0.8.15 (solc-js) Node v16.15.1 Web3.js v1.7.4
I think the issue is with the way how you require them.我认为问题在于您需要它们的方式。 you should not pass the directory path, instead pass the name of the contracts, truffle will assign them.你不应该传递目录路径,而是传递合约的名称,truffle 会分配它们。
let Game = artifacts.require("NameOfGamecontract");
let Character = artifacts.require("NameOfCharacterContract");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.