Sha256: d73aad32d753a72c735197b6fea0633d4924e4755feafec40146cfbf1eca4398

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

# encoding: utf-8
require "spec_helper"

module Okei
  describe FindUnit do

    # ==========================================================================
    # Prepare context
    # ==========================================================================

    let!(:unit) { create :unit, code: "ТЫС М/С2" }
    let!(:text) { "километров в сек" }
    before do
      allow(Corrector::Parse).to receive(:new) do |str, scope:|
        (str == text) && (scope == "okei") ? unit.code : str
      end
    end

    # ==========================================================================
    # Prepare variables
    # ==========================================================================

    let(:listener) { double "listener" }

    def prepare_case(text)
      use_case = FindUnit.new text: text
      use_case.subscribe(listener)
      use_case
    end

    # ==========================================================================
    # Run tests
    # ==========================================================================

    describe "#run" do

      context "with proper text" do

        subject(:use_case) { prepare_case text }

        it "returns the unit" do
          expect(use_case.run).to eq unit
        end

        it "sends 'found' to subscribers" do
          expect(listener).to receive(:found).with(unit)
          use_case.run
        end

        it "caches results of code preparing", :caching do
          use_case.run
          expect(Corrector::Phrase).not_to receive(:scope)
          prepare_case(text).run
        end
      end

      context "with wrong text" do

        subject(:use_case) { prepare_case "тысяч км в сек" }

        it "returns nil" do
          expect(use_case.run).to be_nil
        end

        it "sends 'not_found' to subscribers" do
          expect(listener).to receive :not_found do |messages|
            expect(messages).not_to be_blank
          end
          use_case.run
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
okei-1.0.2 spec/use_cases/okei/find_unit_spec.rb
okei-1.0.1 spec/use_cases/okei/find_unit_spec.rb
okei-1.0.0 spec/use_cases/okei/find_unit_spec.rb
okei-1.0.0.pre.rc spec/use_cases/okei/find_unit_spec.rb