27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Viewing projects<br />

Factories allow you to create new example objects for all of your models in a simple<br />

and elegant syntax. This functionality doesn’t come with Rails, unfortunately, so you<br />

must rely on a gem for it: factory_girl.<br />

4.1.2 The Factory Girl<br />

The factory_girl, created by thoughtbot, 1 provides an easy way to use factories to create<br />

new objects for your tests. Factories define a bunch of default values for an object,<br />

allowing you to have easily craftable objects you can use to run your tests on.<br />

Before you can use this gem, you need to add it to the :test group in your Gemfile.<br />

Now the whole group looks like this:<br />

group :test do<br />

gem 'cucumber-rails'<br />

gem 'capybara'<br />

gem 'database_cleaner'<br />

gem 'factory_girl'<br />

end<br />

To install, run bundle. You’ll now use Factory Girl in your new step definition.<br />

Create a new file at features/step_definitions/project_steps.rb, and add this small<br />

chunk of code:<br />

Given /^there is a project called "([^\"]*)"$/ do |name|<br />

Factory(:project, :name => name)<br />

end<br />

The Factory method 2 looks for the :project factory and generates a new object based<br />

on the details it contains. You don’t have a factory defined yet, but you will shortly.<br />

When you define the factory, you give it a default name. The :name => name part<br />

of this method call changes the default name to the one passed in from your feature.<br />

You use factories here because you needn’t be concerned about any other attribute on<br />

the Project object. If you weren’t using factories, you’d have to use this method to<br />

create the object instead:<br />

Project.create(:name => name)<br />

Although this code is about the same length as its Factory variant, it isn’t futureproof.<br />

If you were to add another field to the projects table and add a validation (say,<br />

a presence one) for that field, you’d have to change all occurrences of the create<br />

method to contain this new field. When you use factories, you can change it in one<br />

place—where the factory is defined. If you cared about what that field was set to, you<br />

could modify it by passing it as one of the key-value pairs in the Factory call.<br />

That’s a lot of theory—now how about some practice? Let’s see what happens<br />

when you run bin/cucumber features/viewing_projects.feature:<br />

Not registered: project (ArgumentError)<br />

1 Thoughtbot’s website: http://thoughtbot.com.<br />

2 Yes, methods can begin with a capital letter.<br />

85

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!