forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix custom validator not ending with _validator (ruby-grape#2337)
* Remove ! Add spec * Changelog entry * Update Changelog's entry Update spec comment Add spec for anonymous class * Fix cop
- Loading branch information
1 parent
d1dfdcc
commit 1147658
Showing
3 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Grape::Validations::Validators::Base do | ||
describe '#inherited' do | ||
context 'when validator is anonymous' do | ||
subject(:custom_validator) { Class.new(described_class) } | ||
|
||
it 'does not register the validator' do | ||
expect(Grape::Validations).not_to receive(:register_validator) | ||
custom_validator | ||
end | ||
end | ||
|
||
# Anonymous class does not have a name and class A < B would leak. | ||
# Simulates inherited callback | ||
context "when validator's underscored name does not end with _validator" do | ||
subject(:custom_validator) { described_class.inherited(TestModule::CustomValidatorABC) } | ||
|
||
before { stub_const('TestModule::CustomValidatorABC', Class.new) } | ||
|
||
it 'registers the custom validator with a short name' do | ||
expect(Grape::Validations).to receive(:register_validator).with('custom_validator_abc', TestModule::CustomValidatorABC) | ||
custom_validator | ||
end | ||
end | ||
|
||
context "when validator's underscored name ends with _validator" do | ||
subject(:custom_validator) { described_class.inherited(TestModule::CustomValidator) } | ||
|
||
before { stub_const('TestModule::CustomValidator', Class.new) } | ||
|
||
it 'registers the custom validator with short name not ending with validator' do | ||
expect(Grape::Validations).to receive(:register_validator).with('custom', TestModule::CustomValidator) | ||
custom_validator | ||
end | ||
end | ||
end | ||
end |