演習 - スマート コントラクトをテストする

完了

このパートでは、出荷コントラクト用の新しいテストを JavaScript で記述します。 代わりに Solidity を使用してテストを記述することもできますが、JavaScript の方がよく使用されます。 スマート コントラクトをテストするには、Truffle を使用します。

テストの開始

まず、新しいテスト ファイルを作成しましょう。

  1. [エクスプローラー] ペインで、テスト フォルダーの上にマウス ポインターを置き、右クリックします。 [新しいファイル] を選び、TestShipping.js という新しいファイルを作成します。

  2. 次のコードをコピーし、テスト ファイルに貼り付けます。

    const ShippingStatus= artifacts.require("./Shipping.sol");
    contract('Shipping', () => {
    
      it("should return the status Pending", async ()=> {
        // Instance of our deployed contract
        const instance = await ShippingStatus.deployed();
        // Checking the initial status in our contract
        const status = await instance.Status();
        // Checking if the status is initially Pending as set in the constructor
        assert.equal(status, "Pending");
      });
    it("should return the status Shipped", async ()=> {
    // Instance of our deployed contract
        const instance = await ShippingStatus.deployed();
    
        // Calling the Shipped() function
        await instance.Shipped();
    
        // Checking the initial status in our contract
        const status = await instance.Status();
    
        // Checking if the status is Shipped
        assert.equal(status, "Shipped");
      });
    
        it("should return the status Delivered", async ()=> {
    
        // Instance of our deployed contract
        const instance = await ShippingStatus.deployed();
    
        // Calling the Shipped() function
        await instance.Delivered();
    
        // Checking the initial status in our contract
        const status = await instance.Status();
    
        // Checking if the status is Delivered
        assert.equal(status, "Delivered");
      });
    });
    

イベントのテスト

truffle-assertions パッケージを使用して、コントラクトによって送信されたイベントをテストします。 このパッケージを使用することで、トランザクション中にイベントが生成されたことをアサートできます。

  1. ターミナルに「npm install truffle-assertions」と入力して、ライブラリをインストールします。

  2. テスト ファイルの 2 行目 (ShippingStatus コントラクトを要求する後) に次のコードを追加します。

    const truffleAssert = require('truffle-assertions');
    
  3. イベントから想定どおりの説明が返されることを確認するためのテストを追加します。 このテストを、ファイル内の最後のテストの後に配置します。 新しい行 (最終行の一連の右中かっこの直前) に追加します。

      it('should return correct event description', async()=>{
    
        // Instance of our deployed contract
        const instance = await ShippingStatus.deployed();
    
        // Calling the Delivered() function
        const delivered = await instance.Delivered();
    
        // Check event description is correct
        truffleAssert.eventEmitted(delivered, 'LogNewAlert', (event) =>{
          return event.description == 'Your package has arrived';
        });
      });
    
    

async/await の使用

.deployed() 関数では Promise が返されます。 そのため、関数の前で await を使用し、テスト コードの前で async を使用しています。 この設定は、コントラクトがデプロイされた後、Promise が満たされるまでテストを進めることができないことを意味します。

スマート コントラクトのトランザクションはほとんどすべて非同期であるため、このパターンはテストでよく使用されます。 トランザクションが非同期なのは、ブロックチェーン台帳に追加する前に検証またはマイニングする必要があるためです。

全体的に、コントラクトに対して 100% のテスト カバレッジを目指す必要があります。メインの Ethereum ネットワーク、または "メイン ネット" へのデプロイを計画している場合は特にそうです。

テストの実行

ターミナルで、次のように入力します。

truffle test

すべてのテストに成功することを確認できます。

  Contract: HelloBlockchain
    ✓ testing ResponseMessage of HelloBlockchain
    ✓ testing Responder of HelloBlockchain
    ✓ testing RequestMessage of HelloBlockchain
    ✓ testing State of HelloBlockchain
    ✓ testing Requestor of HelloBlockchain
    ✓ testing SendRequest of HelloBlockchain (51ms)
    ✓ testing SendResponse of HelloBlockchain (46ms)

  Contract: Shipping
    ✓ should return the status Pending
    ✓ should return the status Shipped (59ms)
    ✓ should return the status Delivered (58ms)
    ✓ should return correct event description (39ms)