简体   繁体   English

truffle & solidity 在测试中无法访问 function

[英]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.

相关问题 如何为javascript / truffle中的每个测试创建新的以太坊/实体合约 - how to create new ethereum/solidity contract for each test in javascript/truffle 如何使用松露测试代码牢固地读取公共变量? - How to read public variable in solidity with truffle test codes? Solidity & Truffle - 在 Solidity 和 JavaScript 中自动生成测试用例,最好的选择是什么? 利弊? - Solidity & Truffle - Automatic test case generate in Solidity and JavaScript, What is the best option? Props and Cons? 是否有必要发出事件,而没有事件? Solidity Truffle 测试中名为“未发出事件”的错误 - Is it necessary to emit event, while there is no event? Error called "No events were emitted" in Solidity Truffle test 松露JS测试不起作用 - Truffle JS test not working 松露“迁移”——cb 不是函数 - Truffle '"Migrations" -- cb is not a function' 松露测试因“无效的操作码”而失败 - Truffle test failed with "invalid opcode" 松露测试给出“错误:尝试运行调用合同功能的交易,但收件人地址___不是合同地址” - Truffle test gives “Error: Attempting to run transaction which calls a contract function, but recipient address ___ is not a contract address” 智能合约的返回地址函数返回承诺对象而不是松露测试中的地址 - Return address function from smart contract returns promise object instead of address in truffle test 松露 - artifacts.require 不是函数 - truffle - artifacts.require is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM