You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Truffle测试报TypeError:getGreeting().call不是函数的技术求助

解决Truffle测试中TypeError: ...call is not a function的问题

你遇到的错误根源在于,在Truffle中使用async/await调用合约的view函数时,不需要额外追加.call()方法——Truffle的合约实例方法已经默认封装了view函数的只读调用逻辑。

修改后的测试代码:

var Greetings = artifacts.require("Greetings");

contract('Greetings Test', async (accounts) => {
  it("check for greetings message", async () => {
    let greeting = await Greetings.deployed();
    // 移除多余的.call()
    let message = await greeting.getGreeting();
    console.log(message);
  });
});

原因说明:

在早期Truffle版本或回调风格的代码里,可能需要通过.call()明确发起只读调用,但当你使用async/await语法时,Truffle会自动识别函数的view/pure属性,并以正确的方式执行调用。直接用await greeting.getGreeting()就能拿到view函数的返回结果,不需要额外的链式调用。

你的合约本身没有问题,编译成功说明语法和版本兼容都正常,只需要调整测试代码的调用方式即可。

内容的提问来源于stack exchange,提问作者KitKarson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:01:04