松露&坚固性无法访问测试中的功能

发布于 2025-02-13 01:44:59 字数 1495 浏览 0 评论 0原文

我运行命令松露测试后 我有这个错误

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我认为问题在于您的要求方式。您不应该通过目录路径,而是传递合同的名称,松露会分配它们。

let Game = artifacts.require("NameOfGamecontract");
let Character = artifacts.require("NameOfCharacterContract");

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.

let Game = artifacts.require("NameOfGamecontract");
let Character = artifacts.require("NameOfCharacterContract");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文