Skip to content

Commit 7125571

Browse files
Fix license validator spec by stubbing Rails.logger and Rails.root
1 parent 7483624 commit 7125571

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

react_on_rails_pro/spec/react_on_rails_pro/license_validator_spec.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,21 @@
2828
}
2929
end
3030

31+
let(:mock_logger) { instance_double(Logger, error: nil, info: nil) }
32+
let(:mock_root) { instance_double(Pathname, join: config_file_path) }
33+
let(:config_file_path) { instance_double(Pathname, exist?: false) }
34+
3135
before do
3236
described_class.reset!
3337
# Stub the public key constant to use our test key
3438
stub_const("ReactOnRailsPro::LicensePublicKey::KEY", test_public_key)
3539
# Clear ENV variable
3640
ENV.delete("REACT_ON_RAILS_PRO_LICENSE")
41+
42+
# Stub Rails.logger to avoid nil errors in unit tests
43+
allow(Rails).to receive(:logger).and_return(mock_logger)
44+
# Stub Rails.root for config file path tests
45+
allow(Rails).to receive(:root).and_return(mock_root)
3746
end
3847

3948
after do
@@ -89,7 +98,8 @@
8998
end
9099

91100
it "raises error" do
92-
expect { described_class.valid? }.to raise_error(ReactOnRailsPro::Error, /License is missing required expiration field/)
101+
expect { described_class.valid? }
102+
.to raise_error(ReactOnRailsPro::Error, /License is missing required expiration field/)
93103
end
94104

95105
it "includes FREE license information in error message" do
@@ -114,30 +124,31 @@
114124
end
115125

116126
context "with missing license" do
117-
let(:config_path) { double("Pathname", exist?: false) }
118-
119127
before do
120128
ENV.delete("REACT_ON_RAILS_PRO_LICENSE")
121-
allow(Rails.root).to receive(:join).with("config", "react_on_rails_pro_license.key").and_return(config_path)
129+
# config_file_path is already set to exist?: false in the let block
122130
end
123131

124132
it "raises error" do
125133
expect { described_class.valid? }.to raise_error(ReactOnRailsPro::Error, /No license found/)
126134
end
127135

128136
it "includes FREE license information in error message" do
129-
expect { described_class.valid? }.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
137+
expect { described_class.valid? }
138+
.to raise_error(ReactOnRailsPro::Error, /FREE evaluation license/)
130139
end
131140
end
132141

133142
context "with license in config file" do
134-
let(:config_path) { Rails.root.join("config", "react_on_rails_pro_license.key") }
135143
let(:valid_token) { JWT.encode(valid_payload, test_private_key, "RS256") }
144+
let(:file_config_path) { instance_double(Pathname, exist?: true) }
136145

137146
before do
138147
ENV.delete("REACT_ON_RAILS_PRO_LICENSE")
139-
allow(config_path).to receive(:exist?).and_return(true)
140-
allow(File).to receive(:read).with(config_path).and_return(valid_token)
148+
allow(mock_root).to receive(:join)
149+
.with("config", "react_on_rails_pro_license.key")
150+
.and_return(file_config_path)
151+
allow(File).to receive(:read).with(file_config_path).and_return(valid_token)
141152
end
142153

143154
it "returns true" do

0 commit comments

Comments
 (0)