我已经开发了一个简单的smartContract。当我运行truffle测试时,它显示了以下错误:我是新手,所以我无法弄清楚。
PS F:\pracdap>松露试验
Compiling your contracts...
===========================
√ Fetching solc version list from solc-bin. Attempt #1
> Compiling .contractsMigrations.sol
> Compiling .contractsSimpleSmartContract.sol
√ Fetching solc version list from solc-bin. Attempt #1
> Compilation warnings encountered:
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "urce file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more informa
--> /F/pracdap/contracts/SimpleSmartContract.sol
> Artifacts written to C:UsersHPAppDataLocalTemptest--1224-GWVOn3NGyps8
> Compiled successfully using:
- solc: 0.8.3+commit.8d00100c.Emscripten.clang
Contract: SimpleSmartContract
1) should be deployed
> No events were emitted
0 passing (128ms)
1 failing
1) Contract: SimpleSmartContract
should be deployed:
Error: SimpleSmartContract has not been deployed to detected network (network/artifact mismatch)
at Object.checkNetworkArtifactMatch (F:nodenode_modulestrufflebuildwebpack:packagescontractlibutilsindex.js
at Function.deployed (F:nodenode_modulestrufflebuildwebpack:packagescontractlibcontractconstructorMethods.j
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at Context.<anonymous> (testsimpleSmartContract.js:5:33)
实体代码
pragma solidity >=0.4.22 <0.9.0;
contract SimpleSmartContract {
}
节点js。测试代码
const SimpleSmartContract = artifacts.require('SimpleSmartContract');
contract('SimpleSmartContract', () => {
it('should be deployed', async () => {
const simpleSmartContract = await SimpleSmartContract.deployed();
assert(simpleSmartContract.address !== '');
});
});
看起来您没有为SimpleSmartContract
添加迁移文件。在Migrations目录中创建一个名为2_deploy_contracts.js
的文件。在其中添加以下代码:
const SimpleSmartContract=artifacts.require("SimpleSmartContract"(;module.exports=函数(部署程序({deployer.deploy(SimpleSmartContract(;}
然后尝试运行测试。
尝试使用迁移。。。如果显示任何错误,请告诉我。
const SimpleSmartContract = artifacts.require("SimpleSmartContract");
module.exports = async function(deployer, _network, accounts) {
await deployer.deploy(SimpleSmartContract);
};
您应该在迁移文件夹中创建一个新的"迁移文件",名称(可能(为2_deploy_contracts.js.
异步函数参数:
deployer
:合同的部署_network
:指定的网络(truffle config.js(accounts
:部署时访问truffle帐户(I.E Solidity构造函数参数(
参考编号:https://trufflesuite.com/docs/truffle/getting-started/running-migrations.html
在我的情况下,我收到了同样的错误。在阅读了很多网站和答案后,我解决了我的问题,包括在迁移日志控制台上生成的网络id:
MyContract.setNetwork(<network id from console migrate>)