snowblink-factory_girl 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,147 @@
1
+ require(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class IntegrationTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ Factory.define :user, :class => 'user' do |f|
7
+ f.first_name 'Jimi'
8
+ f.last_name 'Hendrix'
9
+ f.admin false
10
+ f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
11
+ end
12
+
13
+ Factory.define Post do |f|
14
+ f.name 'Test Post'
15
+ f.association :author, :factory => :user
16
+ end
17
+
18
+ Factory.define :admin, :class => User do |f|
19
+ f.first_name 'Ben'
20
+ f.last_name 'Stein'
21
+ f.admin true
22
+ f.email { Factory.next(:email) }
23
+ end
24
+
25
+ Factory.sequence :email do |n|
26
+ "somebody#{n}@example.com"
27
+ end
28
+ end
29
+
30
+ def teardown
31
+ Factory.factories.clear
32
+ end
33
+
34
+ context "a generated attributes hash" do
35
+
36
+ setup do
37
+ @attrs = Factory.attributes_for(:user, :first_name => 'Bill')
38
+ end
39
+
40
+ should "assign all attributes" do
41
+ assert_equal [:admin, :email, :first_name, :last_name],
42
+ @attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
43
+ end
44
+
45
+ should "correctly assign lazy, dependent attributes" do
46
+ assert_equal "bill.hendrix@example.com", @attrs[:email]
47
+ end
48
+
49
+ should "override attrbutes" do
50
+ assert_equal 'Bill', @attrs[:first_name]
51
+ end
52
+
53
+ should "not assign associations" do
54
+ assert_nil Factory.attributes_for(:post)[:author]
55
+ end
56
+
57
+ end
58
+
59
+ context "a built instance" do
60
+
61
+ setup do
62
+ @instance = Factory.build(:post)
63
+ end
64
+
65
+ should "not be saved" do
66
+ assert @instance.new_record?
67
+ end
68
+
69
+ should "assign associations" do
70
+ assert_kind_of User, @instance.author
71
+ end
72
+
73
+ should "save associations" do
74
+ assert !@instance.author.new_record?
75
+ end
76
+
77
+ should "not assign both an association and its foreign key" do
78
+ assert_equal 1, Factory.build(:post, :author_id => 1).author_id
79
+ end
80
+
81
+ end
82
+
83
+ context "a created instance" do
84
+
85
+ setup do
86
+ @instance = Factory.create('post')
87
+ end
88
+
89
+ should "be saved" do
90
+ assert !@instance.new_record?
91
+ end
92
+
93
+ should "assign associations" do
94
+ assert_kind_of User, @instance.author
95
+ end
96
+
97
+ should "save associations" do
98
+ assert !@instance.author.new_record?
99
+ end
100
+
101
+ end
102
+
103
+ context "an instance generated by a factory with a custom class name" do
104
+
105
+ setup do
106
+ @instance = Factory.create(:admin)
107
+ end
108
+
109
+ should "use the correct class name" do
110
+ assert_kind_of User, @instance
111
+ end
112
+
113
+ should "use the correct factory definition" do
114
+ assert @instance.admin?
115
+ end
116
+
117
+ end
118
+
119
+ context "an attribute generated by a sequence" do
120
+
121
+ setup do
122
+ @email = Factory.attributes_for(:admin)[:email]
123
+ end
124
+
125
+ should "match the correct format" do
126
+ assert_match /^somebody\d+@example\.com$/, @email
127
+ end
128
+
129
+ context "after the attribute has already been generated once" do
130
+
131
+ setup do
132
+ @another_email = Factory.attributes_for(:admin)[:email]
133
+ end
134
+
135
+ should "match the correct format" do
136
+ assert_match /^somebody\d+@example\.com$/, @email
137
+ end
138
+
139
+ should "not be the same as the first generated value" do
140
+ assert_not_equal @email, @another_email
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+ end
@@ -0,0 +1,42 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => 'sqlite3',
3
+ :database => File.join(File.dirname(__FILE__), 'test.db')
4
+ )
5
+
6
+ class CreateSchema < ActiveRecord::Migration
7
+ def self.up
8
+ create_table :users, :force => true do |t|
9
+ t.string :first_name
10
+ t.string :last_name
11
+ t.string :email
12
+ t.boolean :admin, :default => false
13
+ end
14
+
15
+ create_table :posts, :force => true do |t|
16
+ t.string :name
17
+ t.integer :author_id
18
+ end
19
+
20
+ create_table :business, :force => true do |t|
21
+ t.string :name
22
+ t.integer :owner_id
23
+ end
24
+ end
25
+ end
26
+
27
+ CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
28
+
29
+ class User < ActiveRecord::Base
30
+ validates_presence_of :first_name, :last_name, :email
31
+ has_many :posts, :foreign_key => 'author_id'
32
+ end
33
+
34
+ class Business < ActiveRecord::Base
35
+ validates_presence_of :name, :owner_id
36
+ belongs_to :owner, :class_name => 'User'
37
+ end
38
+
39
+ class Post < ActiveRecord::Base
40
+ validates_presence_of :name, :author_id
41
+ belongs_to :author, :class_name => 'User'
42
+ end
@@ -0,0 +1,76 @@
1
+ require(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class SequenceTest < Test::Unit::TestCase
4
+
5
+ context "a sequence" do
6
+
7
+ setup do
8
+ @sequence = Factory::Sequence.new {|n| "=#{n}" }
9
+ end
10
+
11
+ should "start with a value of 1" do
12
+ assert_equal "=1", @sequence.next
13
+ end
14
+
15
+ context "after being called" do
16
+
17
+ setup do
18
+ @sequence.next
19
+ end
20
+
21
+ should "use the next value" do
22
+ assert_equal "=2", @sequence.next
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ context "defining a sequence" do
30
+
31
+ setup do
32
+ @sequence = mock('sequence')
33
+ @name = :count
34
+ Factory::Sequence.stubs(:new).returns(@sequence)
35
+ end
36
+
37
+ should "create a new sequence" do
38
+ Factory::Sequence.expects(:new).with().returns(@sequence)
39
+ Factory.sequence(@name)
40
+ end
41
+
42
+ should "use the supplied block as the sequence generator" do
43
+ Factory::Sequence.stubs(:new).yields(1)
44
+ yielded = false
45
+ Factory.sequence(@name) {|n| yielded = true }
46
+ assert yielded
47
+ end
48
+
49
+ end
50
+
51
+ context "after defining a sequence" do
52
+
53
+ setup do
54
+ @sequence = mock('sequence')
55
+ @name = :test
56
+ @value = '1 2 5'
57
+
58
+ @sequence. stubs(:next).returns(@value)
59
+ Factory::Sequence.stubs(:new). returns(@sequence)
60
+
61
+ Factory.sequence(@name) {}
62
+ end
63
+
64
+ should "call next on the sequence when sent next" do
65
+ @sequence.expects(:next)
66
+
67
+ Factory.next(@name)
68
+ end
69
+
70
+ should "return the value from the sequence" do
71
+ assert_equal @value, Factory.next(@name)
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,11 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'activerecord'
7
+ require 'factory_girl'
8
+ gem 'thoughtbot-shoulda', ">= 2.0.0"
9
+ require 'shoulda'
10
+ require 'mocha'
11
+ require 'models'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snowblink-factory_girl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Joe Ferris
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: factory_girl provides a framework and DSL for defining and using factories - less error-prone, more explicit, and all-around easier to work with than fixtures.
17
+ email: jferris@thoughtbot.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - Changelog
26
+ - CONTRIBUTION_GUIDELINES.rdoc
27
+ - LICENSE
28
+ - Rakefile
29
+ - README.textile
30
+ - lib/factory_girl/aliases.rb
31
+ - lib/factory_girl/attribute.rb
32
+ - lib/factory_girl/attribute_proxy.rb
33
+ - lib/factory_girl/factory.rb
34
+ - lib/factory_girl/sequence.rb
35
+ - lib/factory_girl.rb
36
+ - test/aliases_test.rb
37
+ - test/attribute_proxy_test.rb
38
+ - test/attribute_test.rb
39
+ - test/factory_test.rb
40
+ - test/integration_test.rb
41
+ - test/models.rb
42
+ - test/sequence_test.rb
43
+ - test/test_helper.rb
44
+ has_rdoc: true
45
+ homepage:
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --line-numbers
49
+ - --inline-source
50
+ - --main
51
+ - README.textile
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: factory_girl provides a framework and DSL for defining and using model instance factories.
73
+ test_files:
74
+ - test/aliases_test.rb
75
+ - test/attribute_proxy_test.rb
76
+ - test/attribute_test.rb
77
+ - test/factory_test.rb
78
+ - test/integration_test.rb
79
+ - test/sequence_test.rb