Factory is a pattern used mainly for testing purposes which uses code rather than fake data as in fixtures to help the population of objects.
Questions tagged [factories]
113 questions
115
votes
11 answers
What is a Factory Design Pattern in PHP?
This confuses me, in the most simplest terms what does it do? Pretend you are explaining to your mother or someone almost please.

JasonDavis
- 48,204
- 100
- 318
- 537
11
votes
2 answers
Django: one-to-one field with factory_boy: UNIQUE constraint failed
I'm trying to make factories for these models with factory_boy:
class Course(models.Model):
group = models.OneToOneField(Group)
title = models.CharField(max_length=1024)
class CourseInfo(models.Model):
course =…

nnyby
- 4,748
- 10
- 49
- 105
8
votes
3 answers
Using PHP Faker in Laravel to generate "unique with" entry when seeding a database using a factory
So Similar to the unique with validation rule (See: https://github.com/felixkiss/uniquewith-validator), I want to know how to generate a entry, where one column is unique with another one. I want to seed my database as follows.
Example:
There are 12…

J. Robinson
- 931
- 4
- 17
- 45
8
votes
1 answer
DDD - How to implement factories
I would like to know how to implement factories in domain driven design. (examples)
Where should be placed interfaces and implementations of factories ?
Do I need to create interfaces for Domain objects which factories creating ?
Do I need to create…

user2149358
- 113
- 1
- 8
7
votes
1 answer
Rails has_and_belongs_to_many is confusing me with fixtures and factories
General Confusion
I have bands which can have 3 genres. I read in a previous SO post that the proper way to handle this is a couple steps:
1) In band.rb
has_and_belongs_to_many :genres
2) Create a band_genres join table
Even after reading the…

Tony
- 18,776
- 31
- 129
- 193
7
votes
2 answers
Creating a Factory association that defaults to nil?
Using the FactoryGirl gem, inside the factories.rb file, how can I create a factory with an association that defaults to nil?
I am thinking something along these lines:
Factory.define :user do |factory|
factory.association :post
…

John
- 13,125
- 14
- 52
- 73
6
votes
4 answers
Use a factory's sequence to generate unique phone numbers
I'm new to TDD, RSpec and factories, and trying to understand how to test that each User's phone number attribute is unique. To do so, I'm trying to use a sequence in my User factory. I'm not having much luck with the following:
FactoryGirl.define…

dougiebuckets
- 2,383
- 3
- 27
- 37
5
votes
1 answer
How to properly randomize data with seed.rb using Factory Girl and random_data?
I gave generating test data a first shot by trying to populate my database with a simple script that creates a sufficient number of records for my models accounting for all dependencies (esp. polymorphism).
This is my seeds.rb
require…

matt_jay
- 1,241
- 1
- 15
- 33
5
votes
4 answers
Understanding Factories and should I use them?
I have never used Factories before for the simple reason, I don't understand when I need them. I have been working on a little game in my spare time, and I decided to implement FMOD for the sound. I looked at a wrapper designed for OpenAL(different…
user470760
5
votes
3 answers
How to throw exceptions for the init() method for servlets
well I'll first show my code:
@Override
public void init() throws ServletException {
super.init();
try {
securityController = SecurityControllerFactory.getInstance().create();
} catch (Exception e) {
…

Arturas M
- 4,120
- 18
- 50
- 80
5
votes
4 answers
C# Static Method vs Object Instance
I'm currently developing a C# MVC REST web api, and am trying to choose between one of two possibilities for our design.
Without getting too deep into our design, we intend to have a class for data access, which we'll call DataSource. Each…

Chris Case
- 236
- 5
- 9
4
votes
2 answers
Change factories path in Laravel 5.2
I'm trying to change my factories directory to a custom path, so I'm using this as I saw in a laracasts thread:
use Illuminate\Database\Eloquent\Factory as Factory;
class FactoryServiceProvider extends ServiceProvider
{
public function…

Gerard Reches
- 3,048
- 3
- 28
- 39
4
votes
1 answer
Symfony 1.4.6 loading factories.yml configuration from task
I have the following configuration set in my factories.yml file...
all:
mailer:
param:
transport:
class: Swift_SendmailTransport
param:
command: /usr/sbin/sendmail -oi -t
...to overcome the 'double dot' issue…

chattsm
- 4,651
- 5
- 19
- 20
4
votes
1 answer
How can I make this simple C# generics factory work?
I have this design:
public interface IFactory {
T Create();
T CreateWithSensibleDefaults();
}
public class AppleFactory : IFactory { ... }
public class BananaFactory : IFactory { ... }
// ...
The fictitious Apple and Banana…

Kevin Brassen
- 53
- 2
4
votes
1 answer
Why are Angular.js Services created with 'new'?
According to this question:
angular.service vs angular.factory
Services are created by angular with the new keyword, meaning a new instance of the returned object.
For example:
app.service('helloWorldService', function() {
this.hello =…

corgrath
- 11,673
- 15
- 68
- 99