Subscribed unsubscribe Subscribe Subscribe

elasticsearch-model レベルでモック・スタブする

tech

github.com

これのモック・スタブを行う。

    RESPONSE = { 'hits' => { 'total' => 123, 'max_score' => 456, 'hits' => [{'_id' => '1', 'foo' => 'bar'}] } }
    RESULTS  = Elasticsearch::Model::Response::Results.new DummyModel, RESPONSE

    setup do
      search   = Elasticsearch::Model::Searching::SearchRequest.new DummyModel, '*'
      search.stubs(:execute!).returns RESPONSE

      response = Elasticsearch::Model::Response::Response.new DummyModel, search
      @records = Elasticsearch::Model::Response::Records.new  DummyModel, response
    end

こう言う感じとテストに書かれてる。

rspec ならこんな感じ。

    RESPONSE = { 'hits' => { 'total' => 123, 'max_score' => 456, 'hits' => [{'_id' => '1', 'foo' => 'bar'}] } }
    RESULTS  = Elasticsearch::Model::Response::Results.new DummyModel, RESPONSE

    setup do
      search   = Elasticsearch::Model::Searching::SearchRequest.new DummyModel, '*'
      allow(search).to receive(:execute!).and_return RESPONSE

      response = Elasticsearch::Model::Response::Response.new DummyModel, search
      @records = Elasticsearch::Model::Response::Records.new  DummyModel, response
    end