require "spec_helper" module Hexx describe Dependencies do # Mocks gem core module to test dependencies from before { module Test; extend Dependencies; end } after { Hexx.send :remove_const, :Test } let(:described_module) { Test } describe ".configure" do it "is defined" do expect(described_module).to respond_to :configure end it "yields block with self" do described_module.configure do |config| expect(config).to eq described_module end end end describe ".depends_on" do it "is defined" do expect(described_module).to respond_to :depends_on end it "accepts symbolic attributes" do expect { described_module.depends_on :item }.not_to raise_error end it "accepts string attributes" do expect { described_module.depends_on "item" }.not_to raise_error end it "accepts arrays" do expect { described_module.depends_on "item", [:item] } .not_to raise_error end it "accepts default option" do expect { described_module.depends_on :item, default: Hexx::Service } .not_to raise_error end it "fails when wrong object given" do expect { described_module.depends_on 1 }.to raise_error TypeError end it "fails when blank object given" do expect { described_module.depends_on "" }.to raise_error ArgumentError end it "adds dependencies getters" do described_module.depends_on :item, "user" expect(described_module).to respond_to :item expect(described_module).to respond_to :user end it "adds dependencies setters" do described_module.depends_on :item, "user" expect(described_module).to respond_to :item= expect(described_module).to respond_to :user= end context ":item" do before { described_module.depends_on :item } it "allows setting string and constantizes it" do described_module.item = "String" expect(described_module.item).to eq String end it "allows setting symbol and constantizes it" do described_module.item = :String expect(described_module.item).to eq String end it "allows setting constant" do described_module.item = String expect(described_module.item).to eq String end it "fails if wrong value type given" do expect { described_module.item = 1 }.to raise_error TypeError end it "fails if blank string given" do expect { described_module.item = "" }.to raise_error ArgumentError end end context ":item, default: Hexx::Service" do before { described_module.depends_on :item, default: "Hexx::Service" } it "sets default value for the item" do expect(described_module.item).to eq Hexx::Service end end end end end