All Projects → jcteague → autofixturejs

jcteague / autofixturejs

Licence: MIT License
genarates random data fixtures for testing

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to autofixturejs

Pifpaf
Python fixtures and daemon managing tools for functional testing
Stars: ✭ 161 (+222%)
Mutual labels:  fixtures
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (+40%)
Mutual labels:  fixtures
fixturez
Easily create and maintain test fixtures in the file system
Stars: ✭ 57 (+14%)
Mutual labels:  fixtures
Alice
Expressive fixtures generator
Stars: ✭ 2,289 (+4478%)
Mutual labels:  fixtures
flextures
This plug-in can load and dump test data in databases, loading function is very flexible, dump function is very simple
Stars: ✭ 21 (-58%)
Mutual labels:  fixtures
main
Mocks Server monorepo
Stars: ✭ 109 (+118%)
Mutual labels:  fixtures
Goldie
Golden file testing for Go
Stars: ✭ 121 (+142%)
Mutual labels:  fixtures
lovely-pytest-docker
Pytest plugin providing the ability to use docker-compose services as fixtures.
Stars: ✭ 73 (+46%)
Mutual labels:  fixtures
sequel-seed
A Sequel extension to make seeds/fixtures manageable like migrations
Stars: ✭ 25 (-50%)
Mutual labels:  fixtures
yii2-faker
Yii 2 Faker extension
Stars: ✭ 99 (+98%)
Mutual labels:  fixtures
Foundry
A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
Stars: ✭ 216 (+332%)
Mutual labels:  fixtures
Alicedatafixtures
Nelmio Alice extension to persist the loaded fixtures.
Stars: ✭ 228 (+356%)
Mutual labels:  fixtures
giulius-selenium-tests
A test harness that allows Selenium tests to be run using JUnit and test fixtures to be created and injected by a WebDriver-aware Guice
Stars: ✭ 12 (-76%)
Mutual labels:  fixtures
Doctrinefixturesbundle
Symfony integration for the doctrine/data-fixtures library
Stars: ✭ 2,174 (+4248%)
Mutual labels:  fixtures
jest-fixtures
Use file system fixtures in Jest
Stars: ✭ 39 (-22%)
Mutual labels:  fixtures
Gonkey
Gonkey - a testing automation tool
Stars: ✭ 162 (+224%)
Mutual labels:  fixtures
interface-forge
Graceful mock-data and fixtures generation using TypeScript
Stars: ✭ 58 (+16%)
Mutual labels:  fixtures
fixtures
🔧 Doctrine Fixtures for Nette Framework
Stars: ✭ 15 (-70%)
Mutual labels:  fixtures
test-tools
Improves PHPUnit testing productivity by adding a service container and self-initializing fakes
Stars: ✭ 25 (-50%)
Mutual labels:  fixtures
pytest-datafixtures
Data fixtures for pytest made simple
Stars: ✭ 24 (-52%)
Mutual labels:  fixtures

Deprecated

I did a complete rewrite of this package and published it with a different name. Efate. It has the same basic api, but is modular and extensible, and easier to use.

autofixturejs Build Status Coverage Status Join the chat at https://gitter.im/autofixturejs/Lobby

AutoFixture is a test fixture library that allows users to define fixtures for testing and populates them with pseudo random data.

This library is inspired by Factory-Girl (and the similar node variations) and NBuilder.

Usages

Using the library consists of two parts, 1) defining the factories and 2) creating them in your tests

Creating a factory

You can define an object factory in one of the ways

  • Array of strings
  • object notation

Fields Defined with an Array

Factory.define('User',['first_name','last_name','email'])

This will create an object with the fields specified in the array

var user = Factory.create('User')

The user object will have the fields specified in the definition with pseudo random values:

{
    first_name: 'first_name1',
    last_name: 'last_name1',
    email: 'email1'
}

When you create another instance of this object with the factory, the numbers will be incremented:

    //second object:
    var user2 = factory.create('User')
    
    {
        first_name: 'first_name2',
        last_name: 'last_name2',
        email: 'email2'
    }

You can also create an array of fixtures, each with with unique values.

var users = factory.createListOf('User',2)

[
    {
        first_name: 'first_name1',
        last_name: 'last_name1',
        email: 'email1'
    },
    {
        first_name: 'first_name2',
        last_name: 'last_name2',
        email: 'email2'
    }

Name Generators

You can generate random names without the generic first_name1 values

var user = factory.create('User',[
  'firstName'.asFirstName(),
  'lastName'.asLastName(),
  'fullName'.asFullName()
]);

Will generate names randomly selected from a list. asFullName() will concatenate a first name and last name.

It currently selects from a list of 25 first and last names. If this is not enough let me know and I will increase the pool size

Overriding values

You can override the values at creation time, allowing you to create a generic fixture and change just the values you need for a specific test.

factory.define('User',[
    'first_name',
    'roles'.asArray(1)
]);

var adminUser = factory.create('User',{roles:['admin']});

Alternatively you can pass a function as the override which will give you more control over the result of the override. This is especially helpful for deeply nested objects.

var user = Factory.create('user', (user)=> {
			user.first_name = 'James';
			user.last_name = 'Kirk';
			user.orders[0] = 'new order';
			return user;
		})

You can append new fields through overrides as well. This is useful to create a fixture that could either be passed to an orm like Mongoose or bookshelf without and id. But if you want to simulated an already persisted fixture, you can an id attribute.

factory.define('User', ['first_name', 'last_name']);

// un-persisted fixture:
var user = factory.create('User'); // result: { first_name: 'first_name1', last_name: 'last_name1' }

// persisted user with an id field
var user = factory.create('User', { id: 1 }); // result: { first_name: 'first_name1', last_name: 'last_name1', id: 1 }

To change the behavior of the factory and return specific data types, several helper methods are added to the string object

Factory.define('User',[
    'first_name',
    'id'.asNumber(),
    'created'.asDate(),
    'roles'.asArray(2),
    'department'.asConstant('Sales'),
    'city'.withValue('MyCity'),
    'has_car'.asBoolean()
    'email'.asEmail(),
    'color'.pickFrom(['green', 'yellow', 'red'])
    ]);
    
//created will be DateTime.now
var user = Factory.create('user')
{
    first_name: 'first_name1',
    id: 1
    created: Date
    roles: ['roles1','roles2'],
    city: 'MyCity1'
}

asConstant will create the same value for all fixtures

Custom generators can be defined as well:

Factory.define('User',[
'first_name',
'email'.as(function(i){ return 'email'+i+'@email.com';});
]);

var user = factory.create('User');

{
    first_name: 'first_name1',
    email: '[email protected]'
}

You can also use other Factories to generate fields

Factory.define('User',[
    'first_name',
    
]);

Factory.define('Order',[
    'id'.asNumber(),
    'order_date'.asDate()
    'user'.fromFixture('User')
]);

Or a list of fixtures

//generates orders property as an array with five Order objects
Factory.define('user',[
  'name'.asFullName(),
  'orders'.asListOfFixtures('Order',5)
]);

Using Objects to Define a Factory

You can also use an object to define your fixtures. When you use an object the values for each field are used to create random data when you create the fixture

factory.define('User',{first_name, 'first', created_at: new Date(), id:1});
var user = factory.create('User');
{
    first_name: 'first1';
    created_at: new Date
    id: 1
}

Creating a Fixtures file

Generally speaking you'll want to put the fixture definitions into a single file and reuse for different tests.

There are several ways to do this, but what has worked best for me is to create a fixtures file, define the fixtures and export the factory.

Create a module that takes the factory as a function dependency

//fixtures.js
=============
var factory = require('autofixture');

factory.define ...

exports.module = factory;

In your test files just require your fixture and use the exported factor

//tests.js
var factory = require('./fixtures')

Now you can use the factory to access your defined fixtures.

describe("my tests",functio(){
    var user = factory.create('user');
    
});

Change Log: -- 1.0 -- Fixed issue with overriding array properties. -- Can now append new properties with overrides.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].