Skip to content

Commit

Permalink
Configure RSpec initialiser
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Oct 26, 2023
1 parent f198bb7 commit 3cabf3f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 69 deletions.
11 changes: 11 additions & 0 deletions lib/kangaru/initialisers/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Kangaru
module Initialisers
module RSpec
if Object.const_defined?(:RSpec)
::RSpec.configure do
Kangaru.env = :test
end
end
end
end
end
4 changes: 0 additions & 4 deletions sig/kangaru/initialisers.rbs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module Kangaru
module Initialisers
module RSpec
end
end
end
156 changes: 91 additions & 65 deletions spec/features/initialiser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,114 @@ class Foobar
RUBY
end

context "when the target gem does not extend the initialiser" do
before do
gem.main_file.write(<<~RUBY)
require "kangaru"
describe "initialising application" do
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

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 "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 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

it "does not set the Kangaru application reference" do
expect { require_gem }.not_to change { Kangaru.application }.from(nil)
it "does not define the run! method in the gem's root module" do
require_gem
expect(SomeGem).not_to respond_to(:run!)
end
end

it "does not define the run! method in the gem's root module" do
require_gem
expect(SomeGem).not_to respond_to(:run!)
end
end
context "when the target gem extends the initialiser" do
before do
gem.main_file.write(<<~RUBY)
require "kangaru"
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

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 "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 "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

it "sets the Kangaru application reference" do
expect { require_gem }
.to change { Kangaru.application }
.from(nil)
.to(a_kind_of(Kangaru::Application))
end
it "defines the run! method in the gem's root module" do
require_gem
expect(SomeGem).to respond_to(:run!)
end

it "defines the run! method in the gem's root module" do
require_gem
expect(SomeGem).to respond_to(:run!)
end
describe "application reference" do
subject(:application) { Kangaru.application }

describe "application reference" do
subject(:application) { Kangaru.application }
before { require_gem }

before { require_gem }
it "sets the application source to the gem main file" do
expect(application.paths.source).to eq(gem.main_file)
end

it "sets the application source to the gem main file" do
expect(application.paths.source).to eq(gem.main_file)
it "sets the application name to the expected value" do
expect(application.paths.name).to eq("some_gem")
end
end
end
end

it "sets the application name to the expected value" do
expect(application.paths.name).to eq("some_gem")
end
describe "setting test environment in RSpec" do
subject(:run_test) { gem.exec("rspec #{test_file}") }

let(:test_file) { gem.spec_path("test_spec") }

before do
gem.spec_path.mkdir unless gem.spec_path.exist?

test_file.write(<<~RUBY)
RSpec.describe do
it "sets the test environment" do
expect(Kangaru).to be_test
end
end
RUBY
end

include_context :kangaru_initialised

it "configures Kangaru to the test environment" do
expect(run_test).not_to include("FAILED")
end
end
end

0 comments on commit 3cabf3f

Please sign in to comment.