thoughtbot/factory_girl — Issue #307: Constructing one-off factories?
Author: fj
Created: 2012-03-06 15:24:46+00:00
State: open

I would like to create one-off factories. That is, the Factory doesn't exist anymore as soon as it's out of scope.

Most factories are defined with something like:

```ruby
Factory.define(:user) do
first_name "James"
last_name "Smith"
end

u = Factory.build(:user)
```

But doing it this way means that `FactoryGirl` holds a reference to the `:user` factory forever. No matter where you are, `Factory.build(:user)` means the one you defined earlier. What I want is something like this:

```ruby
f = Factory.temporary(:user) do
first_name "David"
last_name "Jones"
end

u = f.build
```

As soon as `f` goes out of scope, you can't refer to it anymore, so there will be no danger of polluting your object space with factories that aren't used. Basically, if I want a one-off factory that is only used in a particular test, I don't want it exposed to the rest of my tests. That seems sloppy.

Is that possible?