首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试智能契约时无效的柴属性

测试智能契约时无效的柴属性
EN

Ethereum用户
提问于 2021-12-12 10:08:19
回答 2查看 1.3K关注 0票数 0

硬帽项目中,我使用工作服模拟ERC20合同,使用柴氏曾被称为断言:

代码语言:javascript
复制
    const {expect} = require("chai")
    const {artifacts, ethers} = require("hardhat")
    const {smock} = require("@defi-wonderland/smock")

    it("Should successfuly invoke transfer of given ERC20", async () => {
        //ARRANGE
        const MyContractFactory = await ethers.getContractFactory("MyContract")
        const myContract = await MyContractFactory.deploy()
        await myContract.deployed()

        const fakeERC20 = await smock.fake('IERC20');
        fakeERC20.transfer.returns(true)

        const erc20Contract = fakeERC20.address
        const amount = 5
        const to = '0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8'

        //ACT
        const txReceipt = await myContract.myTransfer(erc20Contract, to, amount)

        //ASSERT
        expect(fakeERC20.transfer).to.have.been.calledOnce
        expect(fakeERC20.transfer).to.have.been.calledWith(to, amount);
    })

我收到一个错误:

代码语言:javascript
复制
Error: Invalid Chai property: calledOnce

我尝试过使用西农并解释了这里,但得到了另一个错误:

代码语言:javascript
复制
TypeError: { [Function]
  _watchable: 
   { callHistory: [ [Object] ],
     name: 'transfer',
     answerByIndex: {},
     answerByArgs: [],
     encoder: [Function],
     defaultAnswer: { value: true, shouldRevert: false } },
  atCall: [Function: bound atCall],
  getCall: [Function: bound getCall],
  returns: [Function: bound returns],
  returnsAtCall: [Function: bound returnsAtCall],
  reverts: [Function: bound reverts],
  revertsAtCall: [Function: bound revertsAtCall],
  whenCalledWith: [Function: bound whenCalledWith],
  reset: [Function: bound reset] } is not a spy or a call to a spy!

有什么想法吗?

EN

回答 2

Ethereum用户

发布于 2021-12-12 21:34:58

通过增加

代码语言:javascript
复制
chai.use(smock.matchers)

所以基本上你应该:

  • 遵循工作服的官方文档(一个很棒的库!)
  • 如果您的测试在Invalid Chai property when calling calledOnce中失败,您应该安装sinonsinon-chai
  • 最终,您的进口看起来或多或少类似于:const chai = require('chai') const sinon = require('sinon') const sinonChai = require('sinon-chai') const {ethers} = require('hardhat') const {smock} = require('@defi-wonderland/smock') const expect = chai.expect chai.use(smock.matchers)
票数 0
EN

Ethereum用户

发布于 2022-05-25 14:21:55

从这里开始这里

您可以通过在测试开始时添加以下内容来解决此问题。chai.config.proxyExcludedKeys.push('calledOnce');

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/115429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档