Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create initialiser integration test #11

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions spec/features/initialiser_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/kangaru/testing/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down