<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PMA Media Group&#187; factory girl</title>
	<atom:link href="http://www.pmamediagroup.com/tag/factory-girl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pmamediagroup.com</link>
	<description>Unique Marketing Techniques and Strategies with Guaranteed Results!</description>
	<lastBuildDate>Thu, 29 Jul 2010 15:34:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tutorial: How to install/setup RSpec (and Factory Girl) in Rails</title>
		<link>http://www.pmamediagroup.com/2009/04/tutorial-install-rspec-rails-factory-girl/</link>
		<comments>http://www.pmamediagroup.com/2009/04/tutorial-install-rspec-rails-factory-girl/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 22:04:56 +0000</pubDate>
		<dc:creator>Alan Carl Mitchell</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[factory girl]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=277</guid>
		<description><![CDATA[This tutorial assumes that you have ruby, rails, and mysql installed on your machine. This tutorial also doesn&#8217;t explain much about RSpec or Factory Girl and will just show you a quick way to get them working in a RoR environment.

 .code_div {   background:#000000;   color:#ffffff;   padding:10px;   margin:15px; [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial assumes that you have ruby, rails, and mysql installed on your machine. This tutorial also doesn&#8217;t explain much about <a href="http://rspec.info/">RSpec</a> or <a href="http://www.thoughtbot.com/projects/factory_girl/">Factory Girl</a> and will just show you a quick way to get them working in a RoR environment.</p>
<p><span id="more-277"></span></p>
<style> .code_div {   background:#000000;   color:#ffffff;   padding:10px;   margin:15px; }</style>
<style>  .step_number {   font-weight: bold;   font-size: 1.2em; } </style>
<style> .file_name_div {   font-weight: bold;   margin:15px; }  </style>
<p><span class="step_number">Create the Rails Project</span><br />
Go to your projects directory and create a rails project. Just for fun, let&#8217;s call it &#8216;bank&#8217;:</p>
<div class="code_div">user@host:~$ cd projects<br />
user@host:~/projects$ rails -d mysql bank<br />
user@host:~/projects$ cd bank<br />
user@host:~/projects/bank$</div>
<p><span class="step_number">Create MySQL Databases</span><br />
Go into mysql and create the two databases that rails has set up the bank project to use:</p>
<div class="code_div">user@host:~/projects/bank$ mysql -u root -p<br />
mysql&gt;create database bank_development<br />
mysql&gt;create database bank_test<br />
mysql&gt;exit<br />
Bye<br />
user@host:~/projects/bank$</div>
<p><span class="step_number">Put Gems Into Environments</span><br />
Edit the files</p>
<div class="file_name_div">config/environments/development.rb</div>
<p>and</p>
<div class="file_name_div">config/environments/test.rb:</div>
<p>and add the following lines:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;thoughtbot-factory_girl&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'factory_girl'</span>, <span style="color:#ff3333; font-weight:bold;">:source</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://gems.github.com'</span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;rspec&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'1.2.2'</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'spec'</span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;rspec-rails&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'1.2.2'</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<p>This sets up the test and development environments to use rspec and factory girl.</p>
<p><span class="step_number">Install the Gems</span><br />
Install the gems referenced in your environments:</p>
<div class="code_div">user@host:~/projects/bank$ rake gems:install</div>
<p>You&#8217;ll see output that says that the gems were installed, if you didn&#8217;t have them already.</p>
<p><span class="step_number">Bootstrap RSpec</span><br />
Do a:</p>
<div class="code_div">user@host:~/projects/bank$ script/generate rspec</div>
<p>You will see the following:</p>
<div class="code_div">user@host:~/projects/bank$ script/generate rspec<br />
exists  lib/tasks<br />
create  lib/tasks/rspec.rake<br />
create  script/autospec<br />
create  script/spec<br />
create  script/spec_server<br />
create  spec<br />
create  spec/rcov.opts<br />
create  spec/spec.opts<br />
create  spec/spec_helper.rb<br />
user@host:~/projects/bank$</div>
<p>This generates some default rspec files and directories that the project will use.</p>
<p><span class="step_number">Generate Model, Test, and Migration with rspec_model</span><br />
Do a:</p>
<div class="code_div">user@host:~/projects/bank$ script/generate rspec_model BankAccount</div>
<p>You will see the following output:</p>
<div class="code_div">user@host:~/projects/bank$ script/generate rspec_model BankAccount<br />
exists  app/models/<br />
create  spec/models/<br />
create  spec/fixtures/<br />
create  app/models/bank_account.rb<br />
create  spec/models/bank_account_spec.rb<br />
create  spec/fixtures/bank_accounts.yml<br />
create  db/migrate<br />
create  db/migrate/20090402160952_create_bank_accounts.rb<br />
user@host:~/projects/bank$</div>
<p>This will create a default test at</p>
<div class="file_name_div">spec/models/bank_account_spec.rb</div>
<p>and a BankAccount model in</p>
<div class="file_name_div">app/models/bank_account.rb</div>
<p>and a migration in</p>
<div class="file_name_div">db/migrate/20090402160952_create_bank_accounts.rb</div>
<p>You can also do a script/generate rspec_scaffold BankAccount.</p>
<p><span class="step_number">Edit the Test</span><br />
Go to</p>
<div class="file_name_div">spec/models/bank_account_spec.rb</div>
<p>and you will see:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/../spec_helper'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
describe BankAccount <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@valid_attributes</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  it <span style="color:#996600;">&quot;should create a new instance given valid attributes&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    BankAccount.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>@valid_attributes<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Since this is a bank account, one of the behaviors that we would expect is to be able to make deposits into it. That being the case, let&#8217;s change the test to</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/../spec_helper'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
describe BankAccount <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@bank_account</span> = BankAccount.<span style="color:#9900CC;">create</span>
    <span style="color:#0066ff; font-weight:bold;">@bank_account</span>.<span style="color:#9900CC;">balance</span> = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  it <span style="color:#996600;">&quot;should increase the balance upon deposit&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@bank_account</span>.<span style="color:#9900CC;">deposit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@bank_account</span>.<span style="color:#9900CC;">balance</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">100</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Also, edit</p>
<div class="file_name_div">spec/spec.opts</div>
<p>and change &#8216;progress&#8217; to &#8217;specdoc&#8217; (because it will make rspec output look a lot better)</p>
<p><span class="step_number">Edit the Migration</span><br />
We need to add the &#8216;balance&#8217; attribute to the model. Go to</p>
<div class="file_name_div">db/migrate/20090402160952_create_bank_accounts</div>
<p>(or some file like that)</p>
<p>and change the migration to</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateBankAccounts <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:bank_accounts</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#CC0066; font-weight:bold;">float</span> <span style="color:#ff3333; font-weight:bold;">:balance</span>
&nbsp;
      t.<span style="color:#9900CC;">timestamps</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:bank_accounts</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><span class="step_number">Edit the Model</span> Now we need to add the deposit method to the BankAccount model. Go to</p>
<div class="file_name_div">app/models/bank_account.rb</div>
<p>Change the class to</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> BankAccount <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> deposit<span style="color:#006600; font-weight:bold;">&#40;</span>amount<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">balance</span> = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">balance</span> <span style="color:#006600; font-weight:bold;">+</span> amount
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><span class="step_number">Migrate the DB, Run RSpec</span> Now, do a</p>
<div class="code_div">user@host:~/projects/bank$ rake db:migrate</div>
<p>and then a</p>
<div class="code_div">user@host:~/projects/bank$ rake spec</div>
<p>You should see:</p>
<div class="code_div">user@host:~/projects/bank$ rake spec<br />
(in /home/osadmin/projects/bank_account/bank)</p>
<p>BankAccount<br />
<span style="color:green;">- should increase the balance upon deposit</span></p>
<p>Finished in 0.066398 seconds</p>
<p><span style="color:green;">1 example, 0 failures</span><br />
user@host:~/projects/bank$</div>
<p>At this point, RSpec is functioning.</p>
<p><span class="step_number">Add Factory Girl</span><br />
Want to add Factory Girl? The environments are already set up and gem is installed. The rest is not hard:</p>
<p>Create a &#8216;factories&#8217; directory under the &#8217;spec&#8217; directory.</p>
<div class="code_div">user@host:~/projects/bank$ mkdir spec/factories<br />
user@host:~/projects/bank$</div>
<p>and make the file</p>
<div class="file_name_div">spec/factories/bank_accounts.rb</div>
<p>and put this inside:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Factory.<span style="color:#9900CC;">define</span> <span style="color:#ff3333; font-weight:bold;">:bank_account</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>ba<span style="color:#006600; font-weight:bold;">|</span>
  ba.<span style="color:#9900CC;">add_attribute</span> <span style="color:#ff3333; font-weight:bold;">:balance</span>, <span style="color:#006666;">0</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><span class="step_number">Edit the Test</span><br />
Open</p>
<div class="file_name_div">spec/models/bank_account_spec.rb</div>
<p>Change the body of the before each block to</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@bank_account</span> = Factory<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:bank_account</span>, <span style="color:#ff3333; font-weight:bold;">:balance</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Since the factory you just made (spec/factories/bank_accounts.rb) automatically sets the balance to 0, you could leave the body with just</p>
<p>@bank_account = Factory(:bank_account)</p>
<p>and it would still work.</p>
<p>The finished <span style="font-weight:bold;">spec/models/bank_account_spec.rb</span> will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/../spec_helper'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
describe BankAccount <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@bank_account</span> = Factory<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:bank_account</span>, <span style="color:#ff3333; font-weight:bold;">:balance</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  it <span style="color:#996600;">&quot;should increase the balance upon deposit&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@bank_account</span>.<span style="color:#9900CC;">deposit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@bank_account</span>.<span style="color:#9900CC;">balance</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">100</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><span class="step_number">Watch RSpec Run With Factory Girl</span><br />
Watch the test run with Factory Girl:</p>
<div class="code_div">user@host:~/projects/bank$ rake spec<br />
(in /home/osadmin/projects/bank_account/bank)</p>
<p>BankAccount<br />
<span style="color:green;">- should increase the balance upon deposit</span></p>
<p>Finished in 0.066398 seconds</p>
<p><span style="color:green;">1 example, 0 failures</span><br />
user@host:~/projects/bank$</div>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/04/tutorial-install-rspec-rails-factory-girl/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/


Served from: www.pmamediagroup.com @ 2010-07-31 10:53:52 -->