問題
當(dāng)我運(yùn)行truffle test
的時(shí)候聊记,有點(diǎn)驚訝地發(fā)現(xiàn)測(cè)試過程里并不需要啟動(dòng)ganache-cli命令行程序变抽。翻看了truffleframework的文檔也是含糊其辭累舷,倒是處處暗示必須使用Ganache或者Truffle Develop
作為測(cè)試的運(yùn)行時(shí)傲霸。
所以我開始動(dòng)手做試驗(yàn)宿刮。
試驗(yàn)
先去測(cè)試文件xxContractTest.js中使用console.log(accounts)
打印輸出accounts岂嗓。重復(fù)運(yùn)行測(cè)試后汁展,結(jié)果始終是一致,如下:
[ '0x627306090abaB3A6e1400e9345bC60c78a8BEf57',
'0xf17f52151EbEF6C7334FAD080c5704D77216b732',
'0xC5fdf4076b8F3A5357c5E395ab970B5B54098Fef',
'0x821aEa9a577a9b44299B9c15c88cf3087F3b5544',
'0x0d1d4e623D10F9FBA5Db95830F7d3839406C6AF2',
'0x2932b7A2355D6fecc4b5c0B6BD44cC31df247a2e',
'0x2191eF87E392377ec08E7c08Eb105Ef5448eCED5',
'0x0F4F2Ac550A1b4e2280d04c21cEa7EBD822934b5',
'0x6330A553Fc93768F612722BB8c2eC78aC90B3bbc',
'0x5AEDA56215b167893e80B4fE645BA6d5Bab767DE' ]
而且測(cè)試中打印出了運(yùn)行時(shí)的網(wǎng)絡(luò)始終是test厌殉,如下:
$ truffle test
Using network 'test'
我對(duì)這個(gè)網(wǎng)絡(luò)比較困惑食绿,原因是該項(xiàng)目的配置文件truffle.js
并沒有聲明名稱為test的網(wǎng)絡(luò)。所以我需要更多的信息年枕,通過幫助命令./node_modules/.bin/truffle help test
炫欺,得知了一下可選參數(shù):
Usage: truffle test [<test_file>] [--compile-all] [--network <name>] [--verbose-rpc] [--show-events]
Description: Run JavaScript and Solidity tests
Options:
<test_file>
Name of the test file to be run. Can include path information if the file does not exist in the
current directory.
--compile-all
Compile all contracts instead of intelligently choosing which contracts need to be compiled.
--network <name>
Specify the network to use, using artifacts specific to that network. Network name must exist
in the configuration.
--verbose-rpc
Log communication between Truffle and the Ethereum client.
--show-events
Log all contract events.
其中--network <name>
引起了我的注意。我開始試驗(yàn)不同網(wǎng)絡(luò)熏兄,比如:truffle test --network develop
品洛,結(jié)果有點(diǎn)意外:
Using network 'develop'.
Contract: xxContract
[ '0x627306090abaB3A6e1400e9345bC60c78a8BEf57',
'0xf17f52151EbEF6C7334FAD080c5704D77216b732',
'0xC5fdf4076b8F3A5357c5E395ab970B5B54098Fef',
'0x821aEa9a577a9b44299B9c15c88cf3087F3b5544',
'0x0d1d4e623D10F9FBA5Db95830F7d3839406C6AF2',
'0x2932b7A2355D6fecc4b5c0B6BD44cC31df247a2e',
'0x2191eF87E392377ec08E7c08Eb105Ef5448eCED5',
'0x0F4F2Ac550A1b4e2280d04c21cEa7EBD822934b5',
'0x6330A553Fc93768F612722BB8c2eC78aC90B3bbc',
'0x5AEDA56215b167893e80B4fE645BA6d5Bab767DE' ]
前后對(duì)比,不難發(fā)現(xiàn)摩桶,兩次輸出的賬戶是一致的桥状。我稍微思考了一下,覺得是這可能是truffle test
的fallback機(jī)制硝清,因?yàn)榕渲梦募?code>truffle.js確實(shí)沒有任何設(shè)置develop的網(wǎng)絡(luò)辅斟。
供開發(fā)測(cè)試用的是local網(wǎng)絡(luò),配置如下芦拿,所以我重新針對(duì)該網(wǎng)絡(luò)運(yùn)行測(cè)試士飒。
local: {
host: '127.0.0.1',
port: 8545,
network_id: '*'
}
$ truffle test --network local
此時(shí)運(yùn)行出錯(cuò),錯(cuò)誤是連接不上Ethereum Client蔗崎,這是符合期望的行為酵幕。
Could not connect to your Ethereum client with the following parameters:
- host > 127.0.0.1
- port > 8545
- network_id > *
Please check that your Ethereum client:
- is running
- is accepting RPC connections (i.e., "--rpc" option is used in geth)
- is accessible over the network
- is properly configured in your Truffle configuration file (truffle-config.js)
Truffle v5.0.5 (core: 5.0.5)
Node v10.15.3
那么,究竟Truffle test的fallback機(jī)制是怎樣的呢缓苛?我們需要去源碼中尋找答案芳撒。
解釋
Truffle的命令組織,結(jié)構(gòu)比較簡(jiǎn)單未桥,可以快速定位到文件笔刹,如:trufflesuite/truffle/packages/truffle-core/lib/commands/test.js。其fallback機(jī)制集中在run方法中冬耿。
if (config.networks[config.network]) {
Environment.detect(config, environmentCallback);
} else {
var ipcOptions = {
network: "test"
};
var ganacheOptions = {
host: "127.0.0.1",
port: 7545,
network_id: 4447,
mnemonic:
"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat",
gasLimit: config.gas,
noVMErrorsOnRPCResponse: true
};
Develop.connectOrStart(ipcOptions, ganacheOptions, function(
started,
disconnect
) {
ipcDisconnect = disconnect;
Environment.develop(config, ganacheOptions, environmentCallback);
});
}
});
Develop.connectOrStart(...)
方法其實(shí)和執(zhí)行truffle develop
是相同的操作凤壁。所以不難知道斟览,truffle test
是通過truffle develop
啟動(dòng)新的節(jié)點(diǎn)钻蹬,然后設(shè)置了一系列可用的accounts。
為了進(jìn)一步驗(yàn)證材料讶凉,可以驗(yàn)證下助記詞candy maple cake sugar pudding cream honey rich smooth crumble sweet treat
是否會(huì)產(chǎn)生上述的十個(gè)賬號(hào)地址。
驗(yàn)證的方式很簡(jiǎn)單山孔,拷貝助記詞黏貼到在線bip39網(wǎng)站里懂讯,選擇ETH-Ethereum作為Coin,然后觀察輸出的地址台颠,確認(rèn)確實(shí)符合期望褐望。
2019-04-02