---
title: Short, Explicit Test Setups
teaser:
tags: web,ruby,testing
author: Dan Croak
published_on: 2012-03-12
---

You probably know about this [Factory Bot](http://github.com/thoughtbot/factory_bot)[^1]
definition syntax:

    FactoryBot.define do
      factory :user do
        name { 'Connie Customer' }
      end
    end

But did you know about this Factory Bot invocation syntax?

    setup do
      @user = create(:user)
    end

Or:

    setup do
      @user = build(:user)
    end

Or:

    setup do
      post :create, user: attributes_for(:user)
    end

It's [in there](https://github.com/thoughtbot/factory_bot/blob/master/lib/factory_bot/syntax/methods.rb).

Configuration for Test::Unit / [Shoulda](http://github.com/thoughtbot/shoulda):

    class ActiveSupport::TestCase
      include FactoryBot::Syntax::Methods
    end

Configuration for RSpec:

    RSpec.configure do |config|
      config.include FactoryBot::Syntax::Methods
    end

Configuration for Cucumber:

    World FactoryBot::Syntax::Methods

[^1]: Looking for FactoryGirl? The library was renamed in 2017.
[Project name history can be found here.](https://github.com/thoughtbot/factory_bot/blob/master/NAME.md)
