Sha256: 845f71e6084f3389b1bf879effd913b54696a157bc1fbe14f181e1db56583745

Contents?: true

Size: 1.31 KB

Versions: 8

Compression:

Stored size: 1.31 KB

Contents

---
layout: default
title: Usage
nav_order: 3
has_children: true
---

# Usage

FakerMaker generates factories that build disposable objects for testing. Each factory has a name and a set of attributes.

```ruby
FakerMaker.factory :user do 
  name {'Patsy Stone'}
  email {'patsy@fabulous.co.uk'}
  admin {false}
end
```

This will generate a `User` class with the attributes `name`, `email` and `admin` which will always return the same value.

It is possible to explicitly set the name of class which is particularly useful if there is a risk of redefining an existing one.

```ruby
FakerMaker.factory :user, class: 'EmailUser' do 
  name {'Patsy Stone'}
  email {'patsy@fabulous.co.uk'}
  admin {false}
end
```

The class name will always be turned into a Ruby-style class name so `email_user` would become `EmailUser`.

Because of the block syntax in Ruby, defining attributes as `Hash`es requires two sets of curly brackets:

```ruby
FakerMaker.factory :response do 
  body { { title: 'First Post', content: 'This is part of a hash' } }
end
```

Blocks are executed in the context of their instance. This means you can refer to variables already defined:

```ruby
FakerMaker.factory :user, class: 'EmailUser' do 
  title {'Ms'}
  name {'Patsy Stone'}
  formal_name {"#{title} #{name}"}
  email {'patsy@fabulous.co.uk'}
  admin {false}
end
```

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
faker_maker-2.0.0 docs/usage/index.md
faker_maker-1.3.0 docs/usage/index.md
faker_maker-1.2.1 docs/usage/index.md
faker_maker-1.2.0 docs/usage/index.md
faker_maker-1.1.10 docs/usage/index.md
faker_maker-1.1.9 docs/usage/index.md
faker_maker-1.1.8 docs/usage/index.md
faker_maker-1.1.7 docs/usage/index.md