factory_girl-preload 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e77301eb1edb16a785cb0dbcd0d628bbd2081808
4
+ data.tar.gz: d1594901d3ed7e85a6175d92c6f71945cfe06eab
5
+ SHA512:
6
+ metadata.gz: 5dae2a72f6e597ae5e8126cc56f25c34fd12170e5e6ade711b369f7853adca2829268821a72430be83983327cb69018f76ab1d46cc2824d37449bc467c0712cd
7
+ data.tar.gz: d24f97ef86de07b1b10f0a623e64d834408a33fe0677ddb5d591ce034abd17e2ab3d4ccc99bbe2ed18dcbbcac0667042c48a388ad817d34a4500adf72c5d3795
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source "https://rubygems.org"
2
2
  gemspec
data/Gemfile.lock CHANGED
@@ -1,82 +1,69 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_girl-preload (2.0.2)
4
+ factory_girl-preload (2.1.0)
5
5
  factory_girl (>= 3.0)
6
6
 
7
7
  GEM
8
- remote: http://rubygems.org/
8
+ remote: https://rubygems.org/
9
9
  specs:
10
- actionpack (3.2.8)
11
- activemodel (= 3.2.8)
12
- activesupport (= 3.2.8)
13
- builder (~> 3.0.0)
10
+ actionpack (4.0.3)
11
+ activesupport (= 4.0.3)
12
+ builder (~> 3.1.0)
14
13
  erubis (~> 2.7.0)
15
- journey (~> 1.0.4)
16
- rack (~> 1.4.0)
17
- rack-cache (~> 1.2)
18
- rack-test (~> 0.6.1)
19
- sprockets (~> 2.1.3)
20
- activemodel (3.2.8)
21
- activesupport (= 3.2.8)
22
- builder (~> 3.0.0)
23
- activerecord (3.2.8)
24
- activemodel (= 3.2.8)
25
- activesupport (= 3.2.8)
26
- arel (~> 3.0.2)
27
- tzinfo (~> 0.3.29)
28
- activesupport (3.2.8)
29
- i18n (~> 0.6)
30
- multi_json (~> 1.0)
31
- arel (3.0.2)
32
- builder (3.0.4)
33
- diff-lcs (1.1.3)
14
+ rack (~> 1.5.2)
15
+ rack-test (~> 0.6.2)
16
+ activemodel (4.0.3)
17
+ activesupport (= 4.0.3)
18
+ builder (~> 3.1.0)
19
+ activerecord (4.0.3)
20
+ activemodel (= 4.0.3)
21
+ activerecord-deprecated_finders (~> 1.0.2)
22
+ activesupport (= 4.0.3)
23
+ arel (~> 4.0.0)
24
+ activerecord-deprecated_finders (1.0.3)
25
+ activesupport (4.0.3)
26
+ i18n (~> 0.6, >= 0.6.4)
27
+ minitest (~> 4.2)
28
+ multi_json (~> 1.3)
29
+ thread_safe (~> 0.1)
30
+ tzinfo (~> 0.3.37)
31
+ arel (4.0.2)
32
+ atomic (1.1.15)
33
+ builder (3.1.4)
34
+ diff-lcs (1.2.5)
34
35
  erubis (2.7.0)
35
- factory_girl (3.6.1)
36
+ factory_girl (4.4.0)
36
37
  activesupport (>= 3.0.0)
37
- hike (1.2.1)
38
- i18n (0.6.1)
39
- journey (1.0.4)
40
- json (1.7.5)
41
- multi_json (1.3.6)
42
- mysql2 (0.3.11)
43
- rack (1.4.1)
44
- rack-cache (1.2)
45
- rack (>= 0.4)
46
- rack-ssl (1.3.2)
47
- rack
38
+ i18n (0.6.9)
39
+ minitest (4.7.5)
40
+ multi_json (1.9.0)
41
+ mysql2 (0.3.15)
42
+ rack (1.5.2)
48
43
  rack-test (0.6.2)
49
44
  rack (>= 1.0)
50
- railties (3.2.8)
51
- actionpack (= 3.2.8)
52
- activesupport (= 3.2.8)
53
- rack-ssl (~> 1.3.2)
45
+ railties (4.0.3)
46
+ actionpack (= 4.0.3)
47
+ activesupport (= 4.0.3)
54
48
  rake (>= 0.8.7)
55
- rdoc (~> 3.4)
56
- thor (>= 0.14.6, < 2.0)
57
- rake (0.9.2.2)
58
- rdoc (3.12)
59
- json (~> 1.4)
60
- rspec (2.11.0)
61
- rspec-core (~> 2.11.0)
62
- rspec-expectations (~> 2.11.0)
63
- rspec-mocks (~> 2.11.0)
64
- rspec-core (2.11.1)
65
- rspec-expectations (2.11.3)
66
- diff-lcs (~> 1.1.3)
67
- rspec-mocks (2.11.3)
68
- rspec-rails (2.11.4)
49
+ thor (>= 0.18.1, < 2.0)
50
+ rake (10.1.1)
51
+ rspec-core (2.14.8)
52
+ rspec-expectations (2.14.5)
53
+ diff-lcs (>= 1.1.3, < 2.0)
54
+ rspec-mocks (2.14.6)
55
+ rspec-rails (2.14.1)
69
56
  actionpack (>= 3.0)
57
+ activemodel (>= 3.0)
70
58
  activesupport (>= 3.0)
71
59
  railties (>= 3.0)
72
- rspec (~> 2.11.0)
73
- sprockets (2.1.3)
74
- hike (~> 1.2)
75
- rack (~> 1.0)
76
- tilt (~> 1.1, != 1.3.0)
77
- thor (0.16.0)
78
- tilt (1.3.3)
79
- tzinfo (0.3.33)
60
+ rspec-core (~> 2.14.0)
61
+ rspec-expectations (~> 2.14.0)
62
+ rspec-mocks (~> 2.14.0)
63
+ thor (0.18.1)
64
+ thread_safe (0.2.0)
65
+ atomic (>= 1.1.7, < 2)
66
+ tzinfo (0.3.39)
80
67
 
81
68
  PLATFORMS
82
69
  ruby
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # factory_girl-preload
2
+
3
+ We all love Rails fixtures because they're fast, but we hate to deal with YAML/CSV/SQL files. Here enters Factory Girl (FG).
4
+
5
+ Now, you can easily create records by using predefined factories. The problem is that hitting the database everytime to create records is pretty slow. And believe me, you'll feel the pain when you have lots of specs.
6
+
7
+ So here enters Factory Girl Preload (FGP). You can define which factories will be preloaded, so you don't have to recreate it every time (that will work for 99.37% of the time, according to statistics I just made up).
8
+
9
+ ## Installation
10
+
11
+ gem install factory_girl-preload
12
+
13
+ ## Usage
14
+
15
+ I'm focusing Rails 3 + RSpec 2 stack, so I can't really guarantee that it will work on other setups. Here's how you get started:
16
+
17
+ Add both FG and FGP to your Gemfile:
18
+
19
+ ```ruby
20
+ source "https://rubygems.org"
21
+
22
+ gem "rails"
23
+ gem "mysql2", "~> 0.2.7"
24
+
25
+ group :test, :development do
26
+ gem "rspec-rails"
27
+ gem "factory_girl"
28
+ gem "factory_girl-preload"
29
+ end
30
+ ```
31
+
32
+ On `spec/spec_helper.rb` file, make sure that transactional fixtures are enabled. Here's is my file without all those RSpec comments:
33
+
34
+ ```ruby
35
+ ENV["RAILS_ENV"] ||= "test"
36
+ require File.expand_path("../../config/environment", __FILE__)
37
+ require "rspec/rails"
38
+
39
+ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
40
+
41
+ RSpec.configure do |config|
42
+ config.use_transactional_fixtures = true
43
+ config.mock_with :rspec
44
+ end
45
+ ```
46
+
47
+ Create your factories on `spec/support/factories.rb`. You may have something like this:
48
+
49
+ ```ruby
50
+ FactoryGirl.define do
51
+ factory :user do
52
+ name "John Doe"
53
+ sequence(:email) {|n| "john#{n}@example.org" }
54
+ sequence(:username) {|n| "john#{n}" }
55
+ password "test"
56
+ password_confirmation "test"
57
+ end
58
+
59
+ factory :projects do
60
+ name "My Project"
61
+ association :user
62
+ end
63
+ end
64
+ ```
65
+
66
+ To define your preloadable factories, just use the `preload` method:
67
+
68
+ ```ruby
69
+ FactoryGirl.define do
70
+ factory :user do
71
+ name "John Doe"
72
+ sequence(:email) {|n| "john#{n}@example.org" }
73
+ sequence(:username) {|n| "john#{n}" }
74
+ password "test"
75
+ password_confirmation "test"
76
+ end
77
+
78
+ factory :projects do
79
+ name "My Project"
80
+ association :user
81
+ end
82
+
83
+ preload do
84
+ factory(:john) { Factory(:user) }
85
+ factory(:myapp) { Factory(:project, user: users(:john)) }
86
+ end
87
+ end
88
+ ```
89
+
90
+ Like Rails fixtures, FGP will define methods for each model. You can use it on your examples and alike.
91
+
92
+ ```ruby
93
+ require "spec_helper"
94
+
95
+ describe User do
96
+ let(:user) { users(:john) }
97
+
98
+ it "returns john's record" do
99
+ users(:john).should be_an(User)
100
+ end
101
+
102
+ it "returns myapp's record" do
103
+ projects(:myapp).user.should == users(:john)
104
+ end
105
+ end
106
+ ```
107
+
108
+ Easy and, probably, faster!
109
+
110
+ == Maintainer
111
+
112
+ * Nando Vieira (http://nandovieira.com.br)
113
+
114
+ == License
115
+
116
+ (The MIT License)
117
+
118
+ Permission is hereby granted, free of charge, to any person obtaining
119
+ a copy of this software and associated documentation files (the
120
+ 'Software'), to deal in the Software without restriction, including
121
+ without limitation the rights to use, copy, modify, merge, publish,
122
+ distribute, sublicense, and/or sell copies of the Software, and to
123
+ permit persons to whom the Software is furnished to do so, subject to
124
+ the following conditions:
125
+
126
+ The above copyright notice and this permission notice shall be
127
+ included in all copies or substantial portions of the Software.
128
+
129
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
130
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
131
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
132
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
133
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
134
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
135
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -9,15 +9,27 @@ module FactoryGirl
9
9
  class << self
10
10
  attr_accessor :preloaders
11
11
  attr_accessor :factories
12
+ attr_accessor :record_ids
13
+ attr_accessor :clean_with
12
14
  end
13
15
 
14
16
  self.preloaders = []
15
17
  self.factories = {}
18
+ self.record_ids = {}
19
+ self.clean_with = :truncation
20
+
21
+ def self.active_record
22
+ ActiveRecord::Base
23
+ end
24
+
25
+ def self.connection
26
+ active_record.connection
27
+ end
16
28
 
17
29
  def self.run
18
30
  helper = Object.new.extend(Helpers)
19
31
 
20
- ActiveRecord::Base.connection.transaction :requires_new => true do
32
+ connection.transaction :requires_new => true do
21
33
  preloaders.each do |block|
22
34
  helper.instance_eval(&block)
23
35
  end
@@ -25,23 +37,36 @@ module FactoryGirl
25
37
  end
26
38
 
27
39
  def self.clean(*names)
28
- query = case ActiveRecord::Base.connection.adapter_name
29
- when "SQLite" then "DELETE FROM %s"
30
- when "PostgreSQL" then "TRUNCATE TABLE %s RESTART IDENTITY CASCADE"
31
- else "TRUNCATE TABLE %s"
40
+ query = case clean_with
41
+ when :truncation then try_truncation_query
42
+ when :deletion then "DELETE FROM %s"
43
+ else raise "Couldn't find #{clean_with} clean type"
32
44
  end
33
- names = ActiveRecord::Base.descendants.collect(&:table_name).uniq if names.empty?
34
- ActiveRecord::Base.connection.disable_referential_integrity do
35
- names.each {|table| ActiveRecord::Base.connection.execute(query % ActiveRecord::Base.connection.quote_table_name(table))}
45
+
46
+ names = active_record.descendants.collect(&:table_name).uniq if names.empty?
47
+
48
+ connection.disable_referential_integrity do
49
+ names.each do |table|
50
+ connection.execute(query % connection.quote_table_name(table))
51
+ end
36
52
  end
37
53
  end
38
54
 
39
55
  def self.reload_factories
40
56
  factories.each do |class_name, group|
41
57
  group.each do |name, factory|
42
- factories[class_name][name] = factory.class.find(factory.id)
58
+ factories[class_name][name] = nil
43
59
  end
44
60
  end
45
61
  end
62
+
63
+ private
64
+ def self.try_truncation_query
65
+ case connection.adapter_name
66
+ when "SQLite" then "DELETE FROM %s"
67
+ when "PostgreSQL" then "TRUNCATE TABLE %s RESTART IDENTITY CASCADE"
68
+ else "TRUNCATE TABLE %s"
69
+ end
70
+ end
46
71
  end
47
72
  end
@@ -31,7 +31,10 @@ module FactoryGirl
31
31
 
32
32
  private
33
33
  def factory_get(name, model)
34
- factory = Preload.factories[model.name][name] rescue nil
34
+ factory = Preload.factories[model.name][name]
35
+ if factory.blank? && Preload.factories[model.name].has_key?(name)
36
+ factory = Preload.factories[model.name][name] = model.find(Preload.record_ids[model.name][name])
37
+ end
35
38
  raise "Couldn't find #{name.inspect} factory for #{model.name.inspect} model" unless factory
36
39
  factory
37
40
  end
@@ -44,6 +47,9 @@ module FactoryGirl
44
47
  record = instance_eval(&block)
45
48
  Preload.factories[record.class.name] ||= {}
46
49
  Preload.factories[record.class.name][name.to_sym] = record
50
+
51
+ Preload.record_ids[record.class.name] ||= {}
52
+ Preload.record_ids[record.class.name][name.to_sym] = record.id
47
53
  end
48
54
  end
49
55
  end
@@ -1,4 +1,4 @@
1
- require 'rspec/core'
1
+ require "rspec/core"
2
2
 
3
3
  RSpec.configure do |config|
4
4
  config.include FactoryGirl::Preload::Helpers
@@ -10,4 +10,4 @@ RSpec.configure do |config|
10
10
  config.before(:each) do
11
11
  FactoryGirl::Preload.reload_factories
12
12
  end
13
- end
13
+ end
@@ -2,8 +2,8 @@ module FactoryGirl
2
2
  module Preload
3
3
  module Version
4
4
  MAJOR = 2
5
- MINOR = 0
6
- PATCH = 2
5
+ MINOR = 1
6
+ PATCH = 0
7
7
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
8
  end
9
9
  end
@@ -9,6 +9,15 @@ describe FactoryGirl::Preload do
9
9
  FactoryGirl::Preload.preloaders.should include(block)
10
10
  end
11
11
 
12
+ it "should lazy load all factories, loading only when used" do
13
+ FactoryGirl::Preload.record_ids['User'][:john].should eq(1)
14
+ FactoryGirl::Preload.factories['User'][:john].should be_nil
15
+
16
+ user = users(:john)
17
+
18
+ FactoryGirl::Preload.factories['User'][:john].should eq(user)
19
+ end
20
+
12
21
  it "injects model methods" do
13
22
  expect { users(:john) }.to_not raise_error
14
23
  end
@@ -33,10 +42,25 @@ describe FactoryGirl::Preload do
33
42
  expect { users(:mary) }.to raise_error(%[Couldn't find :mary factory for "User" model])
34
43
  end
35
44
 
36
- it "removes records" do
37
- User.count.should == 1
38
- FactoryGirl::Preload.clean
39
- User.count.should == 0
45
+ it "raises error for missing clean type" do
46
+ FactoryGirl::Preload.clean_with = :invalid
47
+ expect { FactoryGirl::Preload.clean }.to raise_error(%[Couldn't find invalid clean type])
48
+ end
49
+
50
+ context "removes records" do
51
+ it "with deletion" do
52
+ User.count.should == 1
53
+ FactoryGirl::Preload.clean_with = :deletion
54
+ FactoryGirl::Preload.clean
55
+ User.count.should == 0
56
+ end
57
+
58
+ it "with truncation" do
59
+ User.count.should == 1
60
+ FactoryGirl::Preload.clean_with = :truncation
61
+ FactoryGirl::Preload.clean
62
+ User.count.should == 0
63
+ end
40
64
  end
41
65
 
42
66
  context "reloadable factories" do
metadata CHANGED
@@ -1,94 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_girl-preload
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
5
- prerelease:
4
+ version: 2.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Nando Vieira
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-10-22 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: factory_girl
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '3.0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activerecord
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: actionpack
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec-rails
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: mysql2
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - ">="
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - ">="
92
81
  - !ruby/object:Gem::Version
93
82
  version: '0'
94
83
  description: Preload factories (Factory Girl) just like fixtures. It will be easy
@@ -99,11 +88,11 @@ executables: []
99
88
  extensions: []
100
89
  extra_rdoc_files: []
101
90
  files:
102
- - .gitignore
103
- - .rspec
91
+ - ".gitignore"
92
+ - ".rspec"
104
93
  - Gemfile
105
94
  - Gemfile.lock
106
- - README.rdoc
95
+ - README.md
107
96
  - Rakefile
108
97
  - factory_girl-preload.gemspec
109
98
  - lib/factory_girl-preload.rb
@@ -122,27 +111,26 @@ files:
122
111
  - spec/support/factories.rb
123
112
  homepage: http://rubygems.org/gems/factory_girl-preload
124
113
  licenses: []
114
+ metadata: {}
125
115
  post_install_message:
126
116
  rdoc_options: []
127
117
  require_paths:
128
118
  - lib
129
119
  required_ruby_version: !ruby/object:Gem::Requirement
130
- none: false
131
120
  requirements:
132
- - - ! '>='
121
+ - - ">="
133
122
  - !ruby/object:Gem::Version
134
123
  version: '0'
135
124
  required_rubygems_version: !ruby/object:Gem::Requirement
136
- none: false
137
125
  requirements:
138
- - - ! '>='
126
+ - - ">="
139
127
  - !ruby/object:Gem::Version
140
128
  version: '0'
141
129
  requirements: []
142
130
  rubyforge_project:
143
- rubygems_version: 1.8.23
131
+ rubygems_version: 2.2.2
144
132
  signing_key:
145
- specification_version: 3
133
+ specification_version: 4
146
134
  summary: Preload factories (Factory Girl) just like fixtures. It will be easy and,
147
135
  probably, faster!
148
136
  test_files: []
data/README.rdoc DELETED
@@ -1,134 +0,0 @@
1
- = factory_girl-preload
2
-
3
- We all love Rails fixtures because they're fast, but we hate to deal with YAML/CSV/SQL files. Here enters Factory Girl (FG).
4
-
5
- Now, you can easily create records by using predefined factories. The problem is that hitting the database everytime to create records is pretty slow. And believe me, you'll feel the pain when you have lots of specs.
6
-
7
- So here enters Factory Girl Preload (FGP). You can define which factories will be preloaded, so you don't have to recreate it every time (that will work for 99.37% of the time, according to statistics I just made up).
8
-
9
- == Installation
10
-
11
- gem install factory_girl-preload
12
-
13
- == Usage
14
-
15
- I'm focusing Rails 3 + RSpec 2 stack, so I can't really guarantee that it will work on other setups. Here's how you get started:
16
-
17
- Add both FG and FGP to your Gemfile:
18
-
19
- source :rubygems
20
- gem "rails", "3.0.6"
21
- gem "mysql2", "~> 0.2.7"
22
-
23
- group :test, :development do
24
- gem "rspec-rails", "~> 2.5.0"
25
- gem "factory_girl", "~> 1.3.3"
26
- gem "factory_girl-preload", "~> 0.1.0"
27
- end
28
-
29
- On <tt>spec/spec_helper.rb</tt> file, make sure that transactional fixtures are enabled. Here's is my file without all those RSpec comments:
30
-
31
- ENV["RAILS_ENV"] ||= "test"
32
- require File.expand_path("../../config/environment", __FILE__)
33
- require "rspec/rails"
34
-
35
- Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
36
-
37
- RSpec.configure do |config|
38
- config.use_transactional_fixtures = true
39
- config.mock_with :rspec
40
- end
41
-
42
- Create your factories on <tt>spec/support/factories.rb</tt>. You may have something like this:
43
-
44
- Factory.sequence :email do |n|
45
- "john#{n}@doe.com"
46
- end
47
-
48
- Factory.sequence :username do |n|
49
- "john#{n}"
50
- end
51
-
52
- Factory.define :user do |f|
53
- f.name "John Doe"
54
- f.email { Factory.next(:email) }
55
- f.username { Factory.next(:username) }
56
- f.password "test"
57
- f.password_confirmation "test"
58
- end
59
-
60
- Factory.define :projects do |f|
61
- f.name "My Project"
62
- f.association :user
63
- end
64
-
65
- Finally, you can create your preloadable factories. Put those on <tt>spec/support/preloadable_factories.rb</tt>:
66
-
67
- Factory.preload do
68
- factory(:john) { Factory(:user) }
69
- factory(:myapp) { Factory(:project, :user => users(:john)) }
70
- end
71
-
72
- Like Rails fixtures, FGP will define methods for each model. You can use it on your examples and alike.
73
-
74
- require "spec_helper"
75
-
76
- describe User do
77
- let(:user) { users(:john) }
78
-
79
- it "returns john's record" do
80
- users(:john).should be_an(User)
81
- end
82
-
83
- it "returns myapp's record" do
84
- projects(:myapp).user.should == users(:john)
85
- end
86
- end
87
-
88
- The new Factory Girl syntax is also supported:
89
-
90
- FactoryGirl.define do
91
- factory :user do |f|
92
- f.name "John Doe"
93
- f.sequence(:email) {|n| "john#{n}@doe.com"}
94
- end
95
-
96
- factory :skill do |f|
97
- f.association :user
98
- end
99
-
100
- preload do
101
- # the create method is just a shortcut to FactoryGirl.create
102
- factory(:john) { create(:user) }
103
- factory(:ruby) { create(:skill, :user => users(:john)) }
104
- end
105
- end
106
-
107
- Easy and, probably, faster!
108
-
109
- == Maintainer
110
-
111
- * Nando Vieira (http://nandovieira.com.br)
112
-
113
- == License
114
-
115
- (The MIT License)
116
-
117
- Permission is hereby granted, free of charge, to any person obtaining
118
- a copy of this software and associated documentation files (the
119
- 'Software'), to deal in the Software without restriction, including
120
- without limitation the rights to use, copy, modify, merge, publish,
121
- distribute, sublicense, and/or sell copies of the Software, and to
122
- permit persons to whom the Software is furnished to do so, subject to
123
- the following conditions:
124
-
125
- The above copyright notice and this permission notice shall be
126
- included in all copies or substantial portions of the Software.
127
-
128
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
129
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
130
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
131
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
132
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
133
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
134
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.