松露&坚固性无法访问测试中的功能
我运行命令松露测试后 我有这个错误
typeError:_character.getName不是函数
我的测试文件
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");
});
});
});
的
// 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;
}
}
功能
松露V5.5.20(核心:5.5.20) Ganache v7.2.0 固体 - 0.8.15(SOLC -JS) 节点V16.15.1 web3.js v1.7.4
after I run command truffle test
I got this error
TypeError: _character.getName is not a 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
// 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您的要求方式。您不应该通过目录路径,而是传递合同的名称,松露会分配它们。
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.