# encoding: utf-8 require "spec_helper" module Okei describe FindUnit do # ========================================================================== # Prepare context # ========================================================================== let!(:unit) { create :unit, code: "ТЫС ЧЕЛ-ЧАС" } before do { "1000" => "ТЫС", "ЧЕЛОВЕКО" => "ЧЕЛ", "ЧАСОВ" => "ЧАС", "ТЫС ЧЕЛ ЧАС" => "ТЫС ЧЕЛ-ЧАС" }.each { |synonym, text| create :word, synonym: synonym, text: text } 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 let!(:text) { "1000 человеко-часов" } 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(Word).not_to receive(:find_by_synonym) 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