forked from thoughtbot/factory_bot
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support refering to factory based on class name
Allows for build/creating based on RSpec's described_class, useful since we've deprecated other syntaxes from FG. Closes thoughtbot#386
- Loading branch information
1 parent
e8d32b9
commit f83c27e
Showing
5 changed files
with
62 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module FactoryGirl | ||
class Decorator | ||
class ClassKeyHash < Decorator | ||
def [](key) | ||
@component[symbolized_key key] | ||
end | ||
|
||
def []=(key, value) | ||
@component[symbolized_key key] = value | ||
end | ||
|
||
def key?(key) | ||
@component.key? symbolized_key(key) | ||
end | ||
|
||
private | ||
|
||
def symbolized_key(key) | ||
if key.respond_to?(:to_sym) | ||
key.to_sym | ||
else | ||
key.to_s.underscore.to_sym | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
|
||
describe 'finding factories keyed by class instead of symbol' do | ||
before do | ||
define_model("User") do | ||
attr_accessor :name, :email | ||
end | ||
|
||
FactoryGirl.define do | ||
factory :user do | ||
name 'John Doe' | ||
sequence(:email) {|n| "person#{n}@example.com" } | ||
end | ||
end | ||
end | ||
|
||
it 'allows interaction based on class name' do | ||
user = FactoryGirl.create User, email: 'person@example.com' | ||
user.email.should == 'person@example.com' | ||
user.name.should == 'John Doe' | ||
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