mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Extract logic for checking attribute declaration syntax to separate class
This commit is contained in:
parent
e3f3498a58
commit
8e0d492e6c
2 changed files with 28 additions and 6 deletions
|
@ -29,11 +29,33 @@ module FactoryGirl
|
||||||
private
|
private
|
||||||
|
|
||||||
def ensure_non_attribute_writer!
|
def ensure_non_attribute_writer!
|
||||||
if @name.to_s =~ /=$/
|
NonAttributeWriterValidator.new(@name).validate!
|
||||||
attribute_name = $`
|
end
|
||||||
raise AttributeDefinitionError,
|
|
||||||
"factory_girl uses 'f.#{attribute_name} value' syntax " +
|
class NonAttributeWriterValidator
|
||||||
"rather than 'f.#{attribute_name} = value'"
|
def initialize(method_name)
|
||||||
|
@method_name = method_name.to_s
|
||||||
|
@method_name_setter_match = @method_name.match(/(.*)=$/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate!
|
||||||
|
if method_is_writer?
|
||||||
|
raise AttributeDefinitionError, error_message
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def method_is_writer?
|
||||||
|
!!@method_name_setter_match
|
||||||
|
end
|
||||||
|
|
||||||
|
def attribute_name
|
||||||
|
@method_name_setter_match[1]
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_message
|
||||||
|
"factory_girl uses '#{attribute_name} value' syntax rather than '#{attribute_name} = value'"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ describe FactoryGirl::Attribute do
|
||||||
it { should_not be_association }
|
it { should_not be_association }
|
||||||
|
|
||||||
it "raises an error when defining an attribute writer" do
|
it "raises an error when defining an attribute writer" do
|
||||||
error_message = %{factory_girl uses 'f.test value' syntax rather than 'f.test = value'}
|
error_message = %{factory_girl uses 'test value' syntax rather than 'test = value'}
|
||||||
expect {
|
expect {
|
||||||
FactoryGirl::Attribute.new('test=', false)
|
FactoryGirl::Attribute.new('test=', false)
|
||||||
}.to raise_error(FactoryGirl::AttributeDefinitionError, error_message)
|
}.to raise_error(FactoryGirl::AttributeDefinitionError, error_message)
|
||||||
|
|
Loading…
Add table
Reference in a new issue