From 655ded65a0cdf44437fd824939f9592ace4013c9 Mon Sep 17 00:00:00 2001 From: Chris Welham <71787007+apexatoll@users.noreply.github.com> Date: Sun, 24 Sep 2023 17:57:34 +0100 Subject: [PATCH] Create initialiser integration test --- spec/features/initialiser_spec.rb | 87 +++++++++++++++++++++++++++++++ spec/kangaru/testing/gem_spec.rb | 4 ++ 2 files changed, 91 insertions(+) create mode 100644 spec/features/initialiser_spec.rb diff --git a/spec/features/initialiser_spec.rb b/spec/features/initialiser_spec.rb new file mode 100644 index 0000000..352641b --- /dev/null +++ b/spec/features/initialiser_spec.rb @@ -0,0 +1,87 @@ +RSpec.describe "Initialising Kangaru in a target gem", :with_gem do + subject(:require_gem) { gem.load! } + + before do + gem.gem_file("foobar").write(<<~RUBY) + module SomeGem + class Foobar + end + end + RUBY + end + + context "when the target gem does not extend the initialiser" do + before do + gem.main_file.write(<<~RUBY) + require "kangaru" + + module SomeGem + end + RUBY + end + + it "loads the gem module" do + expect { require_gem } + .to change { Object.const_defined?(:SomeGem) } + .from(false) + .to(true) + end + + it "does not autoload gem files" do + expect { require_gem } + .not_to change { Object.const_defined?("SomeGem::Foobar") } + .from(false) + end + + it "does not set the Kangaru application reference" do + expect { require_gem }.not_to change { Kangaru.application }.from(nil) + end + end + + context "when the target gem extends the initialiser" do + before do + gem.main_file.write(<<~RUBY) + require "kangaru" + + module SomeGem + extend Kangaru::Initialiser + end + RUBY + end + + it "loads the gem module" do + expect { require_gem } + .to change { Object.const_defined?("SomeGem") } + .from(false) + .to(true) + end + + it "autoloads gem files" do + expect { require_gem } + .to change { Object.const_defined?("SomeGem::Foobar") } + .from(false) + .to(true) + end + + it "sets the Kangaru application reference" do + expect { require_gem } + .to change { Kangaru.application } + .from(nil) + .to(a_kind_of(Kangaru::Application)) + end + + describe "application reference" do + subject(:application) { Kangaru.application } + + before { require_gem } + + it "sets the application root dir to the gem lib path" do + expect(application.root_dir).to eq(gem.lib_path) + end + + it "sets the application root file to the main gem file" do + expect(application.root_file).to eq(gem.main_file) + end + end + end +end diff --git a/spec/kangaru/testing/gem_spec.rb b/spec/kangaru/testing/gem_spec.rb index 9bfcb14..31ab414 100644 --- a/spec/kangaru/testing/gem_spec.rb +++ b/spec/kangaru/testing/gem_spec.rb @@ -79,6 +79,10 @@ describe "#load!" do subject(:load!) { gem.load! } + after do + Object.send(:remove_const, :SomeGem) if Object.const_defined?(:SomeGem) + end + context "when gem has not been created" do it "raises an error" do expect { load! }.to raise_error(