# 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