github.com/nycdavid/zeus@v0.0.0-20201208104106-9ba439429e03/rubygem/spec/rails_spec.rb (about) 1 require 'zeus/rails' 2 3 module Zeus 4 describe Rails do 5 subject(:rails) { Rails.new } 6 7 def mock_file_existence(file, result) 8 expect(File).to receive(:exists?).with(file).and_return(result) 9 end 10 11 describe "#test_helper" do 12 context "when ENV['RAILS_TEST_HELPER'] is set" do 13 it "loads the test helper file from the environment variable" do 14 helper = "a_test_helper" 15 allow(ENV).to receive(:[]).with("RAILS_TEST_HELPER").and_return(helper) 16 17 expect(rails).to receive(:require).with(helper) 18 rails.test_helper 19 end 20 end 21 22 context "when using rspec" do 23 context "3.0+" do 24 it "requires rails_helper" do 25 mock_file_existence(ROOT_PATH + "/spec/rails_helper.rb", true) 26 27 expect(rails).to receive(:require).with("rails_helper") 28 29 rails.test_helper 30 end 31 end 32 33 it "requires spec_helper" do 34 mock_file_existence(ROOT_PATH + "/spec/rails_helper.rb", false) 35 mock_file_existence(ROOT_PATH + "/spec/spec_helper.rb", true) 36 37 expect(rails).to receive(:require).with("spec_helper") 38 39 rails.test_helper 40 end 41 end 42 43 context "when using minitest" do 44 it "requires minitest_helper" do 45 mock_file_existence(ROOT_PATH + "/spec/rails_helper.rb", false) 46 mock_file_existence(ROOT_PATH + "/spec/spec_helper.rb", false) 47 mock_file_existence(ROOT_PATH + "/test/minitest_helper.rb", true) 48 49 expect(rails).to receive(:require).with("minitest_helper") 50 51 rails.test_helper 52 end 53 end 54 55 context "when there are no rspec or minitest helpers" do 56 it "requires test_helper" do 57 mock_file_existence(ROOT_PATH + "/spec/rails_helper.rb", false) 58 mock_file_existence(ROOT_PATH + "/spec/spec_helper.rb", false) 59 mock_file_existence(ROOT_PATH + "/test/minitest_helper.rb", false) 60 61 expect(rails).to receive(:require).with("test_helper") 62 63 rails.test_helper 64 end 65 end 66 end 67 68 describe "#gem_is_bundled?" do 69 context "for a bundled gem" do 70 it "returns the bundled gem's version" do 71 allow(File).to receive(:read).and_return(" 72 GEM 73 remote: https://rubygems.org/ 74 specs: 75 exception_notification-rake (0.0.6) 76 exception_notification (~> 3.0.1) 77 rake (>= 0.9.0) 78 rake (10.0.4) 79 ") 80 expect(gem_is_bundled?('rake')).to eq '10.0.4' 81 end 82 end 83 end 84 85 describe "#test" do 86 def expect_minitest_autorun 87 # Zeus::Rails#test_helper will require minitest/unit by default. 88 # We need to catch it first, before setting expectations on 89 # helper_file requires below. 90 expect_any_instance_of(Rails).to receive(:require).with("minitest/autorun") 91 end 92 93 context 'minitest' do 94 before do 95 module Zeus::M 96 end 97 expect(Zeus::M).to receive(:run) 98 end 99 100 it "requires autorun when testing with new minitest" do 101 module ::Minitest 102 end 103 expect_minitest_autorun 104 105 rails.test 106 end 107 108 it "requires autorun when testing with old minitest" do 109 expect_minitest_autorun 110 111 rails.test 112 end 113 end 114 115 context 'rspec' do 116 before do 117 class ::RSpec::Core::Runner 118 end 119 end 120 121 it "calls rspec core runner" do 122 expect(RSpec::Core::Runner).to receive(:invoke) 123 rails.test(['test_spec.rb']) 124 end 125 end 126 end 127 end 128 end