Sha256: db40b071febebe7535f336c7413eec7a96990e2954344380c7abb86739c84542

Contents?: true

Size: 1.72 KB

Versions: 6

Compression:

Stored size: 1.72 KB

Contents

# encoding: utf-8
require "spec_helper"

module Uuids::Services
  describe Add do

    let!(:record) { create :record }
    let!(:params) do
      {
        "record" => record,
        "value"  => "47027502-4574-3434-0547-032453443434",
        "wrong"  => "wrong"
      }
    end

    describe ".new" do

      subject { Add.new params }

      it "sets #record" do
        expect(subject.record).to eq params["record"]
      end

      it "sets #value" do
        expect(subject.value).to eq params["value"]
      end

      it "whitelists params" do
        expect(subject.params).to eq params.slice("record", "value")
      end
    end

    describe "#run" do

      let(:listener) { double "listener" }

      context "with valid params" do

        subject { Add.new params }

        it "adds a uuid to the record" do
          expect { subject.run }.to change { Uuids::Models::Uuid.count }.by 1
          uuid = Uuids::Models::Uuid.last
          expect(uuid.record).to eq params["record"]
          expect(uuid.value).to  eq params["value"]
        end

        it "publishes :created" do
          subject.subscribe listener
          expect(listener).to receive(:created) do |uuid, messages|
            expect(uuid).to eq Uuids::Models::Uuid.last
            expect(messages).not_to be_blank
          end
          subject.run
        end
      end

      context "with a wrong value" do

        subject { Add.new params.merge(value: "wrong") }

        it "doesn't create a uuid" do
          expect { subject.run }.not_to change { Uuids::Models::Uuid.all }
        end

        it "publishes :error" do
          subject.subscribe listener
          expect(listener).to receive(:error)
          subject.run
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
uuids-4.1.3 spec/uuids/services/add_spec.rb
uuids-4.1.2 spec/uuids/services/add_spec.rb
uuids-4.1.1 spec/uuids/services/add_spec.rb
uuids-4.1.0 spec/services/add_spec.rb
uuids-4.0.1 spec/services/add_spec.rb
uuids-4.0.0 spec/services/add_spec.rb