Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move callback validation into Callback
- Loading branch information
Showing
4 changed files
with
28 additions
and
35 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,22 @@ | ||
module FactoryGirl | ||
class Callback | ||
VALID_NAMES = [:after_build, :after_create, :after_stub].freeze | ||
|
||
attr_reader :name, :block | ||
|
||
def initialize(name, block) | ||
@name = name.to_sym | ||
@block = block | ||
check_name | ||
end | ||
|
||
private | ||
|
||
def check_name | ||
unless VALID_NAMES.include?(name) | ||
raise InvalidCallbackNameError, "#{name} is not a valid callback name. " + | ||
"Valid callback names are #{VALID_NAMES.inspect}" | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,16 +1,23 @@ | ||
require 'spec_helper' | ||
|
||
describe FactoryGirl::Callback do | ||
let(:name) { :after_create } | ||
let(:block) { proc { "block" } } | ||
let(:proxy) { stub("proxy") } | ||
it "has a name" do | ||
FactoryGirl::Callback.new(:after_create, lambda {}).name.should == :after_create | ||
end | ||
|
||
subject { FactoryGirl::Callback.new(name, block) } | ||
it "converts strings to symbols" do | ||
FactoryGirl::Callback.new("after_create", lambda {}).name.should == :after_create | ||
end | ||
|
||
its(:name) { should == name } | ||
end | ||
it "allows valid callback names to be assigned" do | ||
FactoryGirl::Callback::VALID_NAMES.each do |callback_name| | ||
expect { FactoryGirl::Callback.new(callback_name, lambda {}) }. | ||
to_not raise_error(FactoryGirl::InvalidCallbackNameError) | ||
end | ||
end | ||
|
||
describe FactoryGirl::Callback, "with a string name" do | ||
subject { FactoryGirl::Callback.new("name", nil) } | ||
its(:name) { should == :name } | ||
it "raises if an invalid callback name is assigned" do | ||
expect { FactoryGirl::Callback.new(:magic_fairies, lambda {}) }. | ||
to raise_error(FactoryGirl::InvalidCallbackNameError, /magic_fairies is not a valid callback name/) | ||
end | ||
end |