Substrate runtime をテストする cargo test

# 背景と目的

Substrate runtime をビルドし、再起動、Web 画面で動作確認という流れで開発することは非常に時間がかかる上に効率が悪い。
substrate-module-new で作成されるテンプレートにはテスト用のコードがある。
cargo test でテストができるはず、、、、しかし、エラーがでてビルドが失敗、テストが実行されない。

cargo test で単体テストができるようにしたい。

# 前提条件

まずはモジュールを追加していない状態でテストできるようにする
(cargo test でエラーがでないようにする)

```bash
$ substrate-node-new bdevux MTDK1
```
```bash:tree
bdevux/
├── build.rs
├── build.sh
├── Cargo.lock
├── Cargo.toml
├── init.sh
├── LICENSE
├── README.md
├── runtime
│   ├── Cargo.lock
│   ├── Cargo.toml
│   ├── src
│   ├── target
│   └── wasm
├── src
│   ├── chain_spec.rs
│   ├── cli.rs
│   ├── error.rs
│   ├── main.rs
│   └── service.rs
└── target
    ├── debug
    ├── release
    └── wasm32-unknown-unknown
```

```
bdevux/runtime/
├── Cargo.lock
├── Cargo.toml
├── src
│   ├── lib.rs
│   └── template.rs
├── target
│   ├── debug
│   └── wasm32-unknown-unknown
└── wasm
    ├── build.sh
    ├── Cargo.lock
    ├── Cargo.toml
    ├── src
    └── target
```

# テスト失敗

```bash
[MTDK1@AWS_WS /projects/Projects/substrate/bdevux/runtime]$ cargo test
   Compiling node-template-runtime v1.0.0 (/projects/Projects/substrate/bdevux/runtime)
error[E0405]: cannot find trait `AuthoritiesApi` in module `consensus_authorities`
   --> src/lib.rs:296:30
    |
296 |     impl consensus_authorities::AuthoritiesApi<Block> for Runtime {
    |                                 ^^^^^^^^^^^^^^ not found in `consensus_authorities`
help: possible candidate is found in another module, you can import it into scope
    |
9   | use consensus_authorities::runtime_decl_for_AuthoritiesApi::AuthoritiesApi;
    |
error[E0405]: cannot find trait `AuthoritiesApi` in module `consensus_authorities`
   --> src/lib.rs:296:30
    |
296 |     impl consensus_authorities::AuthoritiesApi<Block> for Runtime {
    |                                 ^^^^^^^^^^^^^^ not found in `consensus_authorities`
help: possible candidate is found in another module, you can import it into scope
    |
9   | use consensus_authorities::runtime_decl_for_AuthoritiesApi::AuthoritiesApi;
    |

error[E0425]: cannot find function `authorities_call_api_at` in module `consensus_authorities::runtime_decl_for_AuthoritiesApi`
   --> src/lib.rs:231:1
    |
231 | / impl_runtime_apis! {
232 | |     impl runtime_api::Core<Block> for Runtime {
233 | |         fn version() -> RuntimeVersion {
234 | |             VERSION
...   |
300 | |     }
301 | | }
    | |_^ not found in `consensus_authorities::runtime_decl_for_AuthoritiesApi`
help: possible candidate is found in another module, you can import it into scope
    |
9   | use client::runtime_api::runtime_decl_for_Core::authorities_call_api_at;
    |

error[E0425]: cannot find function `authorities_native_call_generator` in module `consensus_authorities::runtime_decl_for_AuthoritiesApi`
   --> src/lib.rs:231:1
    |
231 | / impl_runtime_apis! {
232 | |     impl runtime_api::Core<Block> for Runtime {
233 | |         fn version() -> RuntimeVersion {
234 | |             VERSION
...   |
300 | |     }
301 | | }
    | |_^ not found in `consensus_authorities::runtime_decl_for_AuthoritiesApi`
help: possible candidate is found in another module, you can import it into scope
    |
9   | use client::runtime_api::runtime_decl_for_Core::authorities_native_call_generator;
    |


error[E0425]: cannot find function `authorities_call_api_at` in module `consensus_authorities::runtime_decl_for_AuthoritiesApi`
   --> src/lib.rs:231:1
    |
231 | / impl_runtime_apis! {
232 | |     impl runtime_api::Core<Block> for Runtime {
233 | |         fn version() -> RuntimeVersion {
234 | |             VERSION
...   |
300 | |     }
301 | | }
    | |_^ not found in `consensus_authorities::runtime_decl_for_AuthoritiesApi`
help: possible candidate is found in another module, you can import it into scope
    |
9   | use client::runtime_api::runtime_decl_for_Core::authorities_call_api_at;
    |

error[E0425]: cannot find function `authorities_native_call_generator` in module `consensus_authorities::runtime_decl_for_AuthoritiesApi`
   --> src/lib.rs:231:1
    |
231 | / impl_runtime_apis! {
232 | |     impl runtime_api::Core<Block> for Runtime {
233 | |         fn version() -> RuntimeVersion {
234 | |             VERSION
...   |
300 | |     }
301 | | }
    | |_^ not found in `consensus_authorities::runtime_decl_for_AuthoritiesApi`
help: possible candidate is found in another module, you can import it into scope
    |
9   | use client::runtime_api::runtime_decl_for_Core::authorities_native_call_generator;
    |

warning: use of deprecated item 'sr_api_hidden_includes_IMPL_RUNTIME_APIS::sr_api_client::runtime_api::runtime_decl_for_Core::Core::authorities': Please switch to `AuthoritiesApi`.
   --> src/lib.rs:231:1
    |
231 | / impl_runtime_apis! {
232 | |     impl runtime_api::Core<Block> for Runtime {
233 | |         fn version() -> RuntimeVersion {
234 | |             VERSION
...   |
300 | |     }
301 | | }
    | |_^
    |
    = note: #[warn(deprecated)] on by default

warning: use of deprecated item 'sr_api_hidden_includes_IMPL_RUNTIME_APIS::sr_api_client::runtime_api::runtime_decl_for_Core::Core::authorities': Please switch to `AuthoritiesApi`.
   --> src/lib.rs:231:1
    |
231 | / impl_runtime_apis! {
232 | |     impl runtime_api::Core<Block> for Runtime {
233 | |         fn version() -> RuntimeVersion {
234 | |             VERSION
...   |
300 | |     }
301 | | }
    | |_^
    |
    = note: #[warn(deprecated)] on by default

error: aborting due to 3 previous errors

Some errors occurred: E0405, E0425.
For more information about an error, try `rustc --explain E0405`.
error: aborting due to 3 previous errors

Some errors occurred: E0405, E0425.
For more information about an error, try `rustc --explain E0405`.
error: Could not compile `node-template-runtime`.
warning: build failed, waiting for other jobs to finish...
error: Could not compile `node-template-runtime`.

To learn more, run the command again with --verbose.
```

# 修正

エラーが3つ出ていると表示されました。
一つ目

```bash
error[E0405]: cannot find trait `AuthoritiesApi` in module `consensus_authorities`
   --> src/lib.rs:296:30
    |
296 |     impl consensus_authorities::AuthoritiesApi<Block> for Runtime {
    |                                 ^^^^^^^^^^^^^^ not found in `consensus_authorities`
help: possible candidate is found in another module, you can import it into scope
    |
9   | use consensus_authorities::runtime_decl_for_AuthoritiesApi::AuthoritiesApi;
    |
```


[dependencies.consensus_authorities]
default-features = false

で
[features]
default = ['std']
std = [
...（省略）
]

std の中に consensus_authorities が無い



```toml:bdevux/runtime/Cargo.toml

...（省略）

[features]
default = ['std']
std = [
    'parity-codec/std',
    'primitives/std',
    'client/std',
    'rstd/std',
    'runtime-io/std',
    'support/std',
    'balances/std',
    'executive/std',
    'aura/std',
    'indices/std',
    'primitives/std',
    'system/std',
    'timestamp/std',
    'sudo/std',
    'version/std',
    'serde_derive',
    'serde',
    'safe-mix/std',
    'consensus-aura/std',
    'offchain-primitives/std',
    "consensus_authorities/std",
]
```

    "consensus_authorities/std",

上記を追加しました


    'primitives/std',

これが2回書かれているのが気になります、、、、2回も書かなくていいですよね


# テスト成功

```bash

[MTDK1@AWS_WS /projects/Projects/substrate/bdevux/runtime]$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.69s
     Running target/debug/deps/node_template_runtime-75469e7df0efc938

running 1 test
test template::tests::it_works_for_default_value ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests node-template-runtime

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
```

# おわり

これでいいのでしょうか？
Cargo のドキュメントをしっかり読めば理解できそうですね。
まだまだ std とか cfg とか言う単語が理解しきれていません。