rspec-factory-girl 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Yann Lugrin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ = RSpec - Factory Girl
2
+
3
+ RSpec matcher collection for testing your model built with factory girl.
4
+
5
+ describe Post do
6
+ it { should be_built_by_factory } # Factory named ':post' is used
7
+ it { should be_created_by_factory } # Factory named ':post' is used
8
+
9
+ it { should be_built_by_factory(:my_factory) }
10
+ it { should be_created_by_factory(:my_factory) }
11
+ end
12
+
13
+ == Install
14
+
15
+ Run the following if you haven't already:
16
+
17
+ gem sources -a http://gems.github.com
18
+
19
+ Install the gem:
20
+
21
+ sudo gem install yannlugrin-rspec-factory-girl
22
+
23
+ == Installing via Rails
24
+
25
+ Specify the gem dependency in your config/environment.rb file after 'spec/rails' dependency:
26
+
27
+ config.gem "yannlugrin-rspec-factory-girl", :lib => "rspec/factory_girl", :source => "http://gems.github.com"
28
+
29
+ Then run in terminal:
30
+
31
+ $ rake gems:install
32
+
33
+ === As a Plugin
34
+
35
+ You can also install Remarkable as a plugin:
36
+
37
+ $ script/plugin install git://github.com/yannlugrin/rspec-factory-girl.git
38
+
39
+ Or using git submodules:
40
+
41
+ $ git submodule add git://github.com/yannlugrin/rspec-factory-girl.git vendor/plugins/rspec-factory-girl
42
+
43
+ == Requirements
44
+
45
+ * activerecord >= 2.3.2
46
+ * factory_girl >= 1.2.0
47
+ * rspec >= 1.2.0
48
+ * rspec-rails >= 1.2.0
49
+
50
+ == RubyGems
51
+
52
+ RubyGems is not required by this library, if you have a LoadError when you run rake tasks, use
53
+ follwing command:
54
+
55
+ RUBYOPT="rubygems" rake
56
+
57
+ or export RUBYOPTS before launch rake tasks:
58
+
59
+ RUBYOPT="rubygems"
60
+ export RUBYOPT
61
+
62
+ == More information
63
+
64
+ Github: http://www.github.com/yannlugrin/rspec-factory-girl
65
+ Bug tracking: http://yannlugrin.lighthouseapp.com/projects/27537-rspec-factory-girl/overview
66
+
67
+ == Copyright & License
68
+
69
+ Copyright (c) 2009 Yann Lugrin. See LICENSE for details.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 2
@@ -0,0 +1,11 @@
1
+ require 'spec/factory_girl/matchers'
2
+
3
+ module Spec
4
+ module Rails
5
+ module Example
6
+ class ModelExampleGroup
7
+ include Spec::FactoryGirl::Matchers
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'spec/factory_girl/matchers/base'
2
+ require 'spec/factory_girl/matchers/be_built_by_factory'
3
+ require 'spec/factory_girl/matchers/be_created_by_factory'
@@ -0,0 +1,65 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ module Spec
5
+ module FactoryGirl
6
+ module Matchers
7
+ class Base
8
+ def initialize(factory)
9
+ @factory = factory
10
+ @errors = []
11
+ end
12
+
13
+ def matches?(target)
14
+ prepare_matches(target)
15
+ return false unless @result
16
+
17
+ # record is a kind of target class
18
+ unless @result.kind_of?(@target)
19
+ @errors << "'#{@target}' class expected, but factory return object kind of #{@result.class}"
20
+ return false
21
+ end
22
+
23
+ # record is valid
24
+ unless @result.valid?
25
+ @errors += @result.errors.full_messages
26
+ return false
27
+ end
28
+
29
+ # record can be saved
30
+ begin
31
+ @result.save!
32
+ rescue ActiveRecord::RecordInvalid => e
33
+ @errors << e.to_s
34
+ return false
35
+ end
36
+
37
+ # A second record can be created
38
+ begin
39
+ Factory.create(@factory)
40
+ rescue ActiveRecord::RecordInvalid => e
41
+ @errors << "model has uniqueness validation, use sequence in factory (#{e.to_s})"
42
+ return false
43
+ end
44
+
45
+ @errors.empty?
46
+ end
47
+
48
+ private
49
+
50
+ def prepare_matches(target)
51
+ @target ||= target_class(target)
52
+ @factory ||= default_factory(@target)
53
+ end
54
+
55
+ def default_factory(target)
56
+ target.to_s.underscore.to_sym if target.kind_of? Class
57
+ end
58
+
59
+ def target_class(target)
60
+ target.kind_of?(Class) ? target : target.class
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,31 @@
1
+ module Spec
2
+ module FactoryGirl
3
+ module Matchers
4
+ class BeBuiltByFactory < Spec::FactoryGirl::Matchers::Base
5
+ def description
6
+ "build instance of '#{@target}' by factory '#{@factory}'"
7
+ end
8
+
9
+ def matches?(target)
10
+ prepare_matches(target)
11
+
12
+ @result = Factory.build(@factory)
13
+
14
+ super
15
+ end
16
+
17
+ def failure_message_for_should
18
+ "expected #{@target.inspect} to be build by factory '#{@factory}'\n" + @errors.join("\n")
19
+ end
20
+
21
+ def failure_message_for_should_not
22
+ "expected #{@target.inspect} not to be build by factory '#{@factory}'"
23
+ end
24
+ end
25
+
26
+ def be_built_by_factory(factory = nil)
27
+ BeBuiltByFactory.new(factory)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ module Spec
2
+ module FactoryGirl
3
+ module Matchers
4
+ class BeCreatedByFactory < Spec::FactoryGirl::Matchers::Base
5
+ def description
6
+ "create instance of '#{@target}' by factory '#{@factory}'"
7
+ end
8
+
9
+ def matches?(target)
10
+ prepare_matches(target)
11
+
12
+ begin
13
+ @result = Factory.create(@factory)
14
+ rescue ActiveRecord::RecordInvalid => e
15
+ @errors << e.to_s
16
+ return false
17
+ end
18
+
19
+ super
20
+ end
21
+
22
+ def failure_message_for_should
23
+ "expected #{@target.inspect} to be created by factory '#{@factory}'\n" + @errors.join("\n")
24
+ end
25
+
26
+ def failure_message_for_should_not
27
+ "expected #{@target.inspect} not to be created by factory '#{@factory}'"
28
+ end
29
+ end
30
+
31
+ def be_created_by_factory(factory = nil)
32
+ BeCreatedByFactory.new(factory)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,152 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'spec/factory_girl'
3
+
4
+ describe 'Factory' do
5
+ include Spec::FactoryGirl::Matchers
6
+
7
+ context 'with default factory name if subject is a class' do
8
+ before(:each) do
9
+ define_model(:simple_model)
10
+
11
+ Factory.define :simple_model do |model|
12
+ end
13
+ end
14
+ subject { SimpleModel }
15
+
16
+ it { should be_built_by_factory }
17
+ it { should be_created_by_factory }
18
+ end
19
+
20
+ context 'with default factory name if subject is a instance' do
21
+ before(:each) do
22
+ define_model(:simple_model)
23
+
24
+ Factory.define :simple_model do |model|
25
+ end
26
+ end
27
+ subject { SimpleModel.new }
28
+
29
+ it { should be_built_by_factory }
30
+ it { should be_created_by_factory }
31
+ end
32
+
33
+ context 'with specified factory name' do
34
+ before(:each) do
35
+ define_model(:simple_model)
36
+
37
+ Factory.define :another_model, :class => SimpleModel do |model|
38
+ end
39
+ end
40
+ subject { SimpleModel }
41
+
42
+ it { should be_built_by_factory(:another_model) }
43
+ it { should be_created_by_factory(:another_model) }
44
+ end
45
+
46
+ context 'with another class' do
47
+ before(:each) do
48
+ define_model(:simple_model)
49
+ define_model(:another_model)
50
+
51
+ Factory.define :simple_model, :class => AnotherModel do |model|
52
+ end
53
+ end
54
+ subject { SimpleModel }
55
+
56
+ it { should_not be_built_by_factory(:simple_model) }
57
+ it { should_not be_created_by_factory(:simple_model) }
58
+ end
59
+
60
+ context 'without attribute' do
61
+ before(:each) do
62
+ define_model(:simple_model)
63
+
64
+ Factory.define :simple_model do |model|
65
+ end
66
+ end
67
+ subject { SimpleModel }
68
+
69
+ it { should be_built_by_factory(:simple_model) }
70
+ it { should be_created_by_factory(:simple_model) }
71
+ end
72
+
73
+ context 'with attribute' do
74
+ before(:each) do
75
+ define_model(:simple_model, :name => :string)
76
+
77
+ Factory.define :simple_model do |model|
78
+ end
79
+ end
80
+ subject { SimpleModel }
81
+
82
+ it { should be_built_by_factory(:simple_model) }
83
+ it { should be_created_by_factory(:simple_model) }
84
+ end
85
+
86
+ context 'without all needed attribute' do
87
+ before(:each) do
88
+ define_model(:simple_model, :name => :string) do
89
+ validates_presence_of :name
90
+ end
91
+
92
+ Factory.define :simple_model do |model|
93
+ end
94
+ end
95
+ subject { SimpleModel }
96
+
97
+ it { should_not be_built_by_factory(:simple_model) }
98
+ it { should_not be_created_by_factory(:simple_model) }
99
+ end
100
+
101
+ context 'with all needed attribute' do
102
+ before(:each) do
103
+ define_model(:simple_model, :name => :string) do
104
+ validates_presence_of :name
105
+ end
106
+
107
+ Factory.define :simple_model do |model|
108
+ model.name 'My name'
109
+ end
110
+ end
111
+ subject { SimpleModel }
112
+
113
+ it { should be_built_by_factory(:simple_model) }
114
+ it { should be_created_by_factory(:simple_model) }
115
+ end
116
+
117
+ context 'without sequence for unique attribute' do
118
+ before(:each) do
119
+ define_model(:simple_model, :name => :string) do
120
+ validates_uniqueness_of :name
121
+ end
122
+
123
+ Factory.define :simple_model do |model|
124
+ end
125
+ end
126
+ subject { SimpleModel }
127
+
128
+ it { should_not be_built_by_factory(:simple_model) }
129
+ it { should_not be_created_by_factory(:simple_model) }
130
+ end
131
+
132
+ context 'with sequence for unique attribute' do
133
+ before(:each) do
134
+ define_model(:simple_model, :name => :string) do
135
+ validates_uniqueness_of :name
136
+ end
137
+
138
+ Factory.sequence :name do |n|
139
+ "name #{n}"
140
+ end
141
+
142
+ Factory.define :simple_model do |model|
143
+ model.name { Factory.next(:name) }
144
+ end
145
+ end
146
+ subject { SimpleModel }
147
+
148
+ it { should be_built_by_factory(:simple_model) }
149
+ it { should be_created_by_factory(:simple_model) }
150
+ end
151
+ end
152
+
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
@@ -0,0 +1,67 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
2
+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
+
4
+ require 'spec'
5
+ require 'activerecord'
6
+ require 'factory_girl'
7
+
8
+ # Configuration information
9
+ config = {
10
+ :database => {
11
+ :adapter => 'sqlite3',
12
+ :database => File.expand_path(File.join(File.dirname(__FILE__), 'test.sqlite3')),
13
+ :pool => 5,
14
+ :timeout => 5000
15
+ }
16
+ }
17
+
18
+ # Establish connection to database
19
+ ActiveRecord::Base.establish_connection(config[:database])
20
+
21
+ # Rspec config
22
+ Spec::Runner.configure do |config|
23
+ end
24
+
25
+ # Helpers to create models
26
+ def create_table(table_name, &block)
27
+ connection = ActiveRecord::Base.connection
28
+
29
+ begin
30
+ connection.execute("DROP TABLE IF EXISTS #{table_name}")
31
+ connection.create_table(table_name, &block)
32
+ @created_tables ||= []
33
+ @created_tables << table_name
34
+ connection
35
+ rescue Exception => e
36
+ connection.execute("DROP TABLE IF EXISTS #{table_name}")
37
+ raise e
38
+ end
39
+ end
40
+
41
+ def define_model_class(class_name, &block)
42
+ klass = Class.new(ActiveRecord::Base)
43
+
44
+ silence_warnings do
45
+ Object.const_set(class_name, klass)
46
+ end
47
+
48
+ klass.class_eval(&block) if block_given?
49
+
50
+ @defined_constants ||= []
51
+ @defined_constants << class_name
52
+
53
+ klass
54
+ end
55
+
56
+ def define_model(name, columns = {}, &block)
57
+ class_name = name.to_s.pluralize.classify
58
+ table_name = class_name.tableize
59
+
60
+ create_table(table_name) do |table|
61
+ columns.each do |name, type|
62
+ table.column name, type
63
+ end
64
+ end
65
+
66
+ define_model_class(class_name, &block)
67
+ end
Binary file
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-factory-girl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Yann Lugrin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-09 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: yann.lugrin@sans-savoir.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - LICENSE
25
+ files:
26
+ - README.rdoc
27
+ - VERSION.yml
28
+ - lib/spec
29
+ - lib/spec/factory_girl
30
+ - lib/spec/factory_girl/matchers.rb
31
+ - lib/spec/factory_girl/matchers
32
+ - lib/spec/factory_girl/matchers/base.rb
33
+ - lib/spec/factory_girl/matchers/be_built_by_factory.rb
34
+ - lib/spec/factory_girl/matchers/be_created_by_factory.rb
35
+ - lib/spec/factory_girl.rb
36
+ - spec/factory_girl_spec.rb
37
+ - spec/test.sqlite3
38
+ - spec/spec.opts
39
+ - spec/spec_helper.rb
40
+ - LICENSE
41
+ has_rdoc: true
42
+ homepage: http://github.com/yannlugrin/rspec-factory-girl
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --inline-source
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.1
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Rspec matcher collection for testing your model built with factory girl
68
+ test_files: []
69
+