Skip to content

Commit

Permalink
Update variable name definitions to include any traits passed
Browse files Browse the repository at this point in the history
  • Loading branch information
wireframe committed Oct 10, 2013
1 parent 19fdc64 commit b007566
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -33,7 +33,7 @@ describe User do
context 'when user is inactive' do
# instantiate FactoryGirl :user fixture with custom traits
with :user, :when_inactive
it { user.should be_inactive }
it { user_when_inactive.should be_inactive }
end
end
```
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/rspec/version.rb
@@ -1,5 +1,5 @@
module FactoryGirl
module Rspec
VERSION = "1.0.0"
VERSION = "2.0.0.alpha2"
end
end
25 changes: 21 additions & 4 deletions lib/factory_girl/rspec/with.rb
Expand Up @@ -3,12 +3,29 @@
module FactoryGirl
module Rspec
module With
def with(name, *args)
let(name) { ::FactoryGirl.create(name, *args) }
# define an rspec helper method that lazily creates the referenced
# FactoryGirl fixture (via let)
# example usage:
# with :user
def with(*args)
register_factory 'let', *args
end

def with!(name, *args)
let!(name) { ::FactoryGirl.create(name, *args) }
# define an rspec helper method that eagerly creates the referenced
# FactoryGirl fixture (via let!)
# example usage:
# with! :user
def with!(*args)
register_factory 'let!', *args
end

private

def register_factory(method_name, *args)
variable_name = args.reject {|arg| arg.is_a?(Hash) }.join('_')
send(method_name, variable_name) do
::FactoryGirl.create *args
end
end
end
end
Expand Down

0 comments on commit b007566

Please sign in to comment.