Permalink
Browse files

Replaced custom model factory with factory_girl

  • Loading branch information...
1 parent 09ac5f2 commit 3cc8276baaddaea7167a22910d7e00eb5b208d2c @GavinJoyce committed Feb 6, 2009
View
1 config/environment.rb
@@ -15,6 +15,7 @@
config.time_zone = 'UTC'
config.gem 'wvanbergen-scoped_search', :lib => 'scoped_search', :source => 'http://gems.github.com'
+ config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"
config.action_controller.session = {
:session_key => '_rubyjobs_session',
View
17 spec/controllers/jobs_controller_spec.rb
@@ -1,15 +1,14 @@
require File.join(File.dirname(__FILE__), '..', "spec_helper" )
require File.dirname(__FILE__) + '/../factory'
-include Factory
include AuthenticatedTestHelper
describe JobsController do
integrate_views
describe "the index action" do
before do
- @job = Factory.create_job
+ @job = Factory(:job)
get :index
end
@@ -88,7 +87,7 @@
describe "the show action" do
before do
- @job = Factory.create_job
+ @job = Factory(:job)
get 'show', :id => @job
end
@@ -124,7 +123,7 @@
describe "the edit action" do
describe "with a valid action key" do
before do
- @job = Factory.create_job
+ @job = Factory(:job)
get :edit, { :id => @job, :key => @job.key }
end
@@ -146,7 +145,7 @@
describe "with no action key" do
before do
- @job = Factory.create_job
+ @job = Factory(:job)
get :edit, { :id => @job }
end
@@ -157,7 +156,7 @@
describe "with an invalid action key" do
before do
- @job = Factory.create_job
+ @job = Factory(:job)
get :edit, { :id => @job, :key => "abc#{@job.key}" }
end
@@ -171,7 +170,7 @@
describe "with valid attributes" do
it "should create and redirect to the new job" do
Job.count.should == 0
- post 'create', { :job => Factory.new_job.attributes }
+ post 'create', { :job => Factory.build(:job).attributes }
Job.count.should == 1
response.should be_redirect #TODO: GJ: test for view action
flash[:notice].should_not == nil
@@ -183,13 +182,13 @@
it "should send an email" do
JobMailer.should_receive(:deliver_confirmation)
- post 'create', { :job => Factory.new_job.attributes }
+ post 'create', { :job => Factory.build(:job).attributes }
end
end
describe "with invalid attributes" do
before do
- post 'create', { :job => Factory.new_job(:title => nil).attributes }
+ post 'create', { :job => Factory.build(:job, :title => nil).attributes }
end
it "should not create a job" do
View
49 spec/factory.rb
@@ -1,35 +1,20 @@
-module Factory
- def self.create_job(attributes = {})
- job = Factory.new_job(attributes)
- job.save!
- job
- end
+require 'factory_girl'
- def self.new_job(attributes = {})
- default_attributes = {
- :company => 'ACME Inc.',
- :url => 'www.acme.org',
- :email => 'jobs@acme.org',
- :type_id => Factory.create_type.id,
- :location_id => Factory.create_location.id,
- :title => 'Rails developer required for greenfield project',
- :description => 'We are planning on revamping our online store and need someone to build the Rails application',
- :how_to_apply => 'Email jobs@acme.org or call 01-1234567'
- }
- Job.new default_attributes.merge(attributes)
- end
-
- def self.create_location(attributes = {})
- default_attributes = {
- :name => 'Dublin'
- }
- Location.create! default_attributes.merge(attributes)
- end
+Factory.define :job do |job|
+ job.company 'ACME Inc.'
+ job.url 'www.acme.org'
+ job.email 'jobs@acme.org'
+ job.type_id { |type| type.association(:type) }
+ job.location_id { |location| location.association(:location) }
+ job.title 'Rails developer required for greenfield project'
+ job.description 'We are planning on revamping our online store and need someone to build the Rails application'
+ job.how_to_apply 'Email jobs@acme.org or call 01-1234567'
+end
- def self.create_type(attributes = {})
- default_attributes = {
- :name => 'Contract'
- }
- Type.create! default_attributes.merge(attributes)
- end
+Factory.define :location do |location|
+ location.name 'Dublin'
end
+
+Factory.define :type do |type|
+ type.name 'Contract'
+end
View
3 spec/models/job_mailer_spec.rb
@@ -1,11 +1,10 @@
require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/../factory'
-include Factory
describe JobMailer do
describe "when sending a confirmation email" do
before do
- @job = Factory.create_job(:email => 'someone@somewhere.org')
+ @job = Factory(:job)
@email = JobMailer.create_confirmation(@job)
end
View
52 spec/models/job_spec.rb
@@ -1,80 +1,78 @@
require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/../factory'
-include Factory
describe Job do
describe "when creating a job" do
it "should have a key" do
- @job = Factory.create_job
+ @job = Factory(:job)
@job.key.should_not be_nil
end
-
+
it "should have a unique key" do
- @job1 = Factory.create_job
- @job2 = Factory.create_job
+ @job1 = Factory(:job)
+ @job2 = Factory(:job)
@job1.key.should_not == @job2.key
end
end
-
+
describe "when saving a job" do
it "the key should be the same as before the save" do
- @job1 = Factory.create_job
+ @job1 = Factory(:job)
pre_save_key = @job1.key
@job1.save!
@job1.key.should == pre_save_key
end
end
-
+
describe "validations" do
it "should be valid with valid attributes" do
- @job = Factory.create_job
- @job.should be_valid
+ Factory(:job).should be_valid
end
-
+
it "should ensure that a company is provided" do
- @job = Factory.new_job(:company => nil)
+ @job = Factory.build(:job, :company => nil)
@job.should_not be_valid
@job.errors.on(:company).should_not be_nil
end
-
+
it "should ensure that an email is provided" do
- @job = Factory.new_job(:email => nil)
+ @job = Factory.build(:job, :email => nil)
@job.should_not be_valid
@job.errors.on(:email).should_not be_nil
end
it "should ensure that email is a valid email" do
- @job = Factory.new_job(:email => 'invalid@email')
+ @job = Factory.build(:job, :email => 'invalid@email')
@job.should_not be_valid
@job.errors.on(:email).should_not be_nil
end
it "should ensure that a type_id is provided" do
- @job = Factory.new_job(:type_id => nil)
+ @job = Factory.build(:job, :type_id => nil)
@job.should_not be_valid
@job.errors.on(:type_id).should_not be_nil
end
it "should ensure that a location_id is provided" do
- @job = Factory.new_job(:location_id => nil)
+ @job = Factory.build(:job, :location_id => nil)
@job.should_not be_valid
@job.errors.on(:location_id).should_not be_nil
end
it "should ensure that a title is provided" do
- @job = Factory.new_job(:title => nil)
+ @job = Factory.build(:job, :title => nil)
@job.should_not be_valid
@job.errors.on(:title).should_not be_nil
end
it "should ensure that a description is provided" do
- @job = Factory.new_job(:description => nil)
+ @job = Factory.build(:job, :description => nil)
@job.should_not be_valid
@job.errors.on(:description).should_not be_nil
end
it "should ensure that a how_to_apply is provided" do
- @job = Factory.new_job(:how_to_apply => nil)
+ @job = Factory.build(:job, :how_to_apply => nil)
@job.should_not be_valid
@job.errors.on(:how_to_apply).should_not be_nil
end
@@ -87,39 +85,37 @@
it "should search the company" do
Job.search_for('google').length.should == 0
- Factory.create_job(:company => 'Google')
+ Factory(:job, :company => 'Google')
Job.search_for('google').length.should == 1
end
it "should search the title" do
Job.search_for('contract').length.should == 0
- Factory.create_job(:title => 'Developer needed (contract)')
+ Factory(:job, :title => 'Developer needed (contract)')
Job.search_for('contract').length.should == 1
end
it "should search the description" do
Job.search_for('june 2009').length.should == 0
- Factory.create_job(:description => 'This job starts in June 2009')
+ Factory(:job, :description => 'This job starts in June 2009')
Job.search_for('june 2009').length.should == 1
end
it "should search the how_to_apply" do
Job.search_for('jobs@acme.org').length.should == 0
- Factory.create_job(:how_to_apply => 'email us - jobs@acme.org')
+ Factory(:job, :how_to_apply => 'email us - jobs@acme.org')
Job.search_for('jobs@acme.org').length.should == 1
end
it "should search the location" do
Job.search_for('dublin').length.should == 0
- location = Factory.create_location(:name => 'Dublin')
- Factory.create_job(:location_id => location.id)
+ Factory(:job, :location => Factory(:location, :name => 'Dublin'))
Job.search_for('dublin').length.should == 1
end
it "should search the type" do
Job.search_for('permanent').length.should == 0
- type = Factory.create_type(:name => 'Permanent')
- Factory.create_job(:type_id => type.id)
+ Factory(:job, :type => Factory(:type, :name => 'Permanent'))
Job.search_for('permanent').length.should == 1
end
end
View
20 spec/models/location_spec.rb
@@ -1,26 +1,16 @@
require File.dirname(__FILE__) + '/../spec_helper'
+require File.dirname(__FILE__) + '/../factory'
describe Location do
-
- valid_attributes = {
- :name => 'Dublin'
- }
-
describe "validations" do
- before do
- @location = Location.new
- end
-
it "should be valid with valid attributes" do
- @location.attributes = valid_attributes
- @location.should be_valid
+ Factory.build(:location).should be_valid
end
it "should ensure that a name is provided" do
- @location.attributes = valid_attributes.except(:name)
- @location.should_not be_valid
- @location.errors.on(:name).should_not be_nil
+ location = Factory.build(:location, :name => nil)
+ location.should_not be_valid
+ location.errors.on(:name).should_not be_nil
end
-
end
end
View
22 spec/models/type_spec.rb
@@ -1,26 +1,18 @@
require File.dirname(__FILE__) + '/../spec_helper'
+require File.dirname(__FILE__) + '/../factory'
describe Type do
-
- valid_attributes = {
- :name => 'Contract'
- }
+ #it { should validate_presence_of(:name) } #NOTE: GJ: why is this not working with rspec-rails 1.1.12??
describe "validations" do
- before do
- @type = Type.new
- end
-
it "should be valid with valid attributes" do
- @type.attributes = valid_attributes
- @type.should be_valid
+ Factory.build(:type).should be_valid
end
-
+
it "should ensure that a name is provided" do
- @type.attributes = valid_attributes.except(:name)
- @type.should_not be_valid
- @type.errors.on(:name).should_not be_nil
+ type = Factory.build(:type, :name => nil)
+ type.should_not be_valid
+ type.errors.on(:name).should_not be_nil
end
-
end
end

0 comments on commit 3cc8276

Please sign in to comment.