这是我的单元测试的部分...过去几个小时我一直在解决这个问题。但是我不能...我认为错误在于
const { deployer } = await getNamedAccounts()
这一行。抽奖合同无法识别...(注意-所有部署都正常工作...)
!developmentChains.includes(network.name)
? describe.skip()
: describe("Raffle UNIT test", async () => {
let raffle, vrfCoordinatorV2Mock
const chainId = network.config.chainId
beforeEach(async () => {
const { deployer } = await getNamedAccounts()
await deployments.fixture(["all"])
raffle = await ethers.getContract("Raffle", deployer)
vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock", deployer)
})
describe("Constructor function", async () => {
it("Initializes the raffle correctly.", async () => {
const raffleState = await raffle.getRaffleState()
const interval = await raffle.getInterval()
assert.equal(raffleState.toString(), "0")
assert.equal(interval.toString(), networkConfig[chainId]["interval"])
})
})
})
我遇到的错误...
~/b/JSweb3_2/smart-contract_lottery on main !2 hh test ✔ took 5s system at 07:27:25 am
Raffle UNIT test
Constructor function
Development chain detected! Deploying mocks
Mocks deployed!!!...
---------------------!--!--!---------------------
1) "before each" hook for "Initializes the raffle correctly."
0 passing (428ms)
1 failing
1) Raffle UNIT test
"before each" hook for "Initializes the raffle correctly.":
Error: No Contract deployed with name Raffle
at Object.getContract (node_modules/@nomiclabs/hardhat-ethers/src/internal/helpers.ts:447:11)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runNextTicks (node:internal/process/task_queues:65:3)
at listOnTimeout (node:internal/timers:528:9)
at processTimers (node:internal/timers:502:7)
at Context.<anonymous> (test/unit/Raffle.test.js:16:24)
我正在遵循接线教程。
我相信您正在遵循帕特里克的课程,您在运行测试之前检查过您的部署脚本是否正在执行吗?您可以在脚本中执行控制台日志并确保它们正在导出标签。例如,
module.exports.tags = ['all', 'mocks'];
我在帕特里克的课程中遇到了同样的问题,我已经改变了
getContract("ContractName", 部署者)
到
getContractAt("ContracrName", 部署者)
并且它有效。 我在这里很新手,我不相信这是正确的方法。
我在本主题中找到了不同 getContract 实例的解释: TypeError:无法读取未定义的属性(读取“getContract”)
getContract & getContractFactory & getContractAt 的区别
ethers.getContract() :
返回与contractAddressOrName处的合约的新连接 合约接口。它用于连接到合约
ethers.getContractFactory() :
返回代表智能合约工厂的 JavaScript 对象。 它用于部署智能合约的新实例。
ethers.getContractAt() :
返回已部署的智能合约实例 到以太坊网络。它采用现有的以太坊地址 智能合约作为参数并返回一个代表的对象 这个合同的具体实例。