我已经设置了节点:
nearup run localnet --binary-path ~/code/nearcore/target/release我正在尝试运行一个jest测试用例:
beforeAll(async function () {
// NOTE: nearlib and nearConfig are made available by near-cli/test_environment
const near = await nearlib.connect(nearConfig)
})但是,显然缺少如何在本地节点上创建测试帐户的步骤。这是错误:
● Test suite failed to run
Can not sign transactions for account test.near, no matching key pair found in Signer.
at node_modules/near-api-js/lib/account.js:83:23
at Object.exponentialBackoff [as default] (node_modules/near-api-js/lib/utils/exponential-backoff.js:7:24)
at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:80:24)
at Account.createAndDeployContract (node_modules/near-api-js/lib/account.js:176:9)
at LocalTestEnvironment.setup (node_modules/near-cli/test_environment.js:39:9)看起来near-js-api也被硬编码为只部署一个契约。我需要测试多个合约。如何部署多个合同?来自near-js-api test_environment.js
class LocalTestEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}
async setup() {
this.global.nearlib = require('near-api-js');
this.global.nearAPI = require('near-api-js');
this.global.window = {};
let config = require('./get-config')();
this.global.testSettings = this.global.nearConfig = config;
const now = Date.now();
// create random number with at least 7 digits
const randomNumber = Math.floor(Math.random() * (9999999 - 1000000) + 1000000);
config = Object.assign(config, {
contractName: 'test-account-' + now + '-' + randomNumber,
accountId: 'test-account-' + now + '-' + randomNumber
});
const keyStore = new nearAPI.keyStores.UnencryptedFileSystemKeyStore(PROJECT_KEY_DIR);
config.deps = Object.assign(config.deps || {}, {
storage: this.createFakeStorage(),
keyStore,
});
const near = await nearAPI.connect(config);
const masterAccount = await near.account(testAccountName);
const randomKey = await nearAPI.KeyPair.fromRandom('ed25519');
const data = [...fs.readFileSync('./out/main.wasm')];
await config.deps.keyStore.setKey(config.networkId, config.contractName, randomKey);
await masterAccount.createAndDeployContract(config.contractName, randomKey.getPublicKey(), data, INITIAL_BALANCE);
await super.setup();
}near-js-sdk本身正在针对神秘的共享测试进行部署。
case 'ci':
return {
networkId: 'shared-test',
nodeUrl: 'https://rpc.ci-testnet.near.org',
masterAccount: 'test.near',
};发布于 2020-09-29 09:34:32
如何部署多个合同?
您可以使用与test_environment.js相同的masterAccount.createAndDeployContract。除了一些常见的init之外,这里没有什么特别的--你可以直接创建你的测试所需要的任何账户/契约。
near-js-sdk本身正在针对神秘的共享测试进行部署。
这是可用于运行集成测试的共享网络。除非你想让你的开发工作保持私密性--这是运行测试的推荐方式(因为这是目前可用于测试的最现实的环境)。
但是,显然缺少如何在本地节点上创建测试帐户的步骤。
如果您使用create-near-app创建项目,那么在neardev/项目文件夹中可能已经有了相应的test.near密钥。这就是为什么上面的神秘环境通常是开箱即用的。
对于您的本地环境,您需要自己创建test.near:
NODE_ENV=local near create-account test.near --masterAccount some-existing-account在此之后,您可以将密钥复制为本地格式(或仅重新配置UnencryptedFileSystemKeyStore以使用~/.near-credentials路径):
cp ~/.near-credentials/local/test.near.json project/dir/neardev/local/test.near.jsonhttps://stackoverflow.com/questions/64109797
复制相似问题