ハウツーガイド - トリガー/アクションのテスト作成

このページは機械翻訳により提供されています。翻訳内容と英語版に相違がある場合は、英語版が優先されます。

このセグメントでは、connector内の任意のlambdaに対するテストの作成方法を説明します。 このため、単一のアクションのexecute lambdaに対するテストを作成します。 同じロジックは、残りのlambdaにも適用できます。

サンプルコネクター

connector.rb内のコード。

ruby
{
  title: 'Chargebee-demo',

  connection: {
    fields: [
      {
        name: 'api_key',
        control_type: 'password',
        hint: 'You can find your API key final change3' \
          "under 'Settings'=>'Configure Chargebee'=>'API Keys and Webhooks'" \
          " in Chargebee's web console.",
        label: 'Your API Key'
      },
      {
        name: 'domain',
        control_type: 'subdomain',
        url: 'chargebee.com'
      }
    ],

    authorization: {
      type: 'basic_auth',

      apply: lambda do |connection|
        user(connection['api_key'])
      end
    },

    base_uri: lambda do |connect|
      "https://#{connect['domain']}.chargebee.com"
    end
  },

  test: lambda do |_connection|
    get('/api/v2/plans', limit: 1)
  end,

  actions: {

    search_customers: {
      title: 'Search customers',
      subtitle: 'Search for customers using name',
      description: 'Search customer in Chargebee',

      input_fields: lambda do |_object_definitions|
        [
          {
            name: 'name',
            label: 'Name to query by',
            hint: 'Provide the name of the customer to query'
          },
          {
            name: 'id',
            label: 'Name to query by',
            hint: 'Provide the name of the customer to query'
          }
        ]
      end,

      execute: lambda do |_connection, input, _input_schema, _output_schema|
        get('/api/v2/customers', input)
      end,

      output_fields: lambda do |_object_definitions|
        [
          {
            name: 'first_name'
          },
          {
            name: 'last_name'
          },
          {
            name: 'id'
          }
        ]
      end
    }

  },

}

settings.yaml.enc内の認証情報。

yaml
api_key: valid_api_key
domain: valid_domain

テストの生成

アクションごとに個別のspecファイルを作成することも、workato generate testコマンドを使用してconnectorに基づいてテストを生成することもできます。 これにより、テストの作成を開始するために必要なスタブの大半を含むspecファイルが生成されます。

サンプルRSpecコンテンツ

ruby
RSpec.describe 'actions/search_customers', :vcr do

  subject(:output) { connector.actions.search_customers(input) }

  let(:connector) { Workato::Connector::Sdk::Connector.from_file('connector.rb', settings) }
  let(:settings) { Workato::Connector::Sdk::Settings.from_default_file }
  let(:input) { JSON.parse(File.read('fixtures/actions/search_customers/input.json')) }

  let(:expected_output) { JSON.parse(File.read('fixtures/actions/search_customers/output.json')) }

  describe 'given valid input' do
    it 'gives expected output' do
      expect(output).to eq(expected_output)
    end
  end

  let(:action) { connector.actions.search_customers }

  describe 'execute' do
    subject(:output) { action.execute(settings, input) }
    let(:input) { JSON.parse(File.read('fixtures/actions/search_customers/input_parsed.json')) }
    let(:expected_output) { JSON.parse(File.read('fixtures/actions/search_customers/output.json')) }

    context 'given valid input' do
      it 'gives expected output' do
        expect(output).to eq(expected_output)
      end
    end
  end

  describe 'sample_output' do
    subject(:sample_output) { action.sample_output(settings, input) }

    pending 'add some examples'
  end

  describe 'input_fields' do
    subject(:input_fields) { action.input_fields(settings, config_fields) }

    pending 'add some examples'
  end

  describe 'output_fields' do
    subject(:output_fields) { action.output_fields(settings, config_fields) }

    pending 'add some examples'
  end
end

ここでは、このアクションに対するさまざまなテスト用のスタブがあることがわかります。 これは、Gemを使用してテストを生成すると提供されます。 execute lambdaのみのテストの作成方法を説明します。

ステップ1 - コネクターインスタンスの定義

テストを開始するには、Workato SDK Gemを使用してコネクターのインスタンスを作成する必要があります。

ruby
  let(:connector) { Workato::Connector::Sdk::Connector.from_file('connector.rb', settings) }

ステップ2 - 設定インスタンスの定義

次に、Workato SDK Gemを使用してsettingsのインスタンスを作成する必要があります。 これはWorkato上のコネクションと同義です。 前に定義したコネクターインスタンスでも、この設定インスタンスが使用されることに注意してください。

ruby
  let(:settings) { Workato::Connector::Sdk::Settings.from_default_file }

ステップ3 - アクションの定義

関連するインスタンスを作成したら、残りのテストで参照しやすいようにactionをインスタンス化します。

ruby
  let(:action) { connector.actions.search_customers }

ステップ3 - テストの記述とsubjectの定義

ここでは、実行するテストの"family"を記述します。 この場合、キーワードexecuteを使用します。 その後、テストのsubjectも定義します。 ここでは、outputの値に、connectorインスタンスでsearch_customersアクションのexecute lambdaを実行した結果を割り当てます。 これは、定義済みのaction.execute(settings,input)で行います。

ruby
  describe 'execute' do
    subject(:output) { action.execute(settings, input) }

TIP

テストを生成すると、executeextended_input_schemaextended_output_schemaという2つの追加引数がある理由が気になるかもしれません。 connectorでexecute: lambdaを定義するときと同様に、ここでのexecuteも同じ4つの引数を受け取ることができます。 このアクションでは不要だったため、これらは削除しました。

ステップ4 - 個々のテストのアサーションの宣言

テストが成功または失敗するには、宣言された比較が必要です。

ここでは、有効なinputを渡した既知のレスポンスとexecute lambdaのoutputが等しくなることを"expect"すると宣言しています。 これは、inputexpected_outputという2つの新しい変数を作成して行いました。 expected_outputの生成は手動で行う必要はなく、CLIコマンドを実行してexecute lambdaを呼び出すときに行えます。 例: workato exec actions.search_customers.execute --input='fixtures/actions/search_customers/input.json' --output='fixtures/actions/search_customers/output.json'

ruby
  describe 'execute' do
    subject(:output) { action.execute(settings, input) }
    let(:input) { JSON.parse(File.read('fixtures/actions/search_customers/input.json')) }
    let(:expected_output) { JSON.parse(File.read('fixtures/actions/search_customers/output.json')) }

    context 'given valid input' do
      it 'gives expected output' do
        expect(output).to eq(expected_output)
      end
    end
  end

ステップ5 - RSpecテストの実行

最後のステップは、RSpecテストを実行することです。 これは、bundle exec rspec spec/actions/search_customers_spec.rbコマンドで行います。

shell
$ bundle exec rspec spec/actions/search_customers_spec.rb

actions/search_customers
  execute
    given valid input
      gives expected output
  sample_output
    add some examples (PENDING: Not yet implemented)
  input_fields
    add some examples (PENDING: Not yet implemented)
  output_fields
    add some examples (PENDING: Not yet implemented)

Pending: (Failures listed here are expected and do not affect your suites status)

  1) actions/search_customers sample_output add some examples
     # Not yet implemented
     # ./spec/actions/search_customers_spec.rb:28

  2) actions/search_customers input_fields add some examples
     # Not yet implemented
     # ./spec/actions/search_customers_spec.rb:34

  3) actions/search_customers output_fields add some examples
     # Not yet implemented
     # ./spec/actions/search_customers_spec.rb:40


Finished in 0.03218 seconds (files took 0.90373 seconds to load)
4 examples, 0 failures, 3 pending

Last updated: