factory-girl-matchers 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
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,39 @@
1
+ Factory Girl MAtchers
2
+ =====================
3
+
4
+ [![Build Status](https://secure.travis-ci.org/yannlugrin/factory-girl-matchers.png?branch=master)](http://travis-ci.org/yannlugrin/factory-girl-matchers?branch=master)
5
+
6
+ Test::Unit- and RSpec-compatible one-liners that test your factories build with Factory Girl
7
+
8
+ describe Post do
9
+ it { should be_built_by_factory } # Factory named ':post' is used
10
+ it { should be_created_by_factory } # Factory named ':post' is used
11
+
12
+ it { should be_built_by_factory(:my_factory) }
13
+ it { should be_created_by_factory(:my_factory) }
14
+ end
15
+
16
+ Installation
17
+ ------------
18
+
19
+ ```shell
20
+ gem install factory-girl-matchers
21
+ ```
22
+ or add the following line to Gemfile:
23
+
24
+ ```ruby
25
+ gem 'factory-girl-matchers'
26
+ ```
27
+ and run `bundle install` from your shell.
28
+
29
+ More Information
30
+ ----------------
31
+
32
+ * [Rubygems](https://rubygems.org/gems/factory-girl-matchers)
33
+ * [Code](http://www.github.com/yannlugrin/factory-girl-matchers)
34
+ * [Issues](https://github.com/yannlugrin/factory-girl-matchers/issues)
35
+
36
+ == Copyright & License
37
+
38
+ Copyright (c) 2012 Yann Lugrin. See LICENSE for details.
39
+
@@ -0,0 +1,2 @@
1
+ require 'factory_girl/matchers'
2
+
@@ -0,0 +1,9 @@
1
+ require 'factory_girl/matchers/be_built_by_factory'
2
+ require 'factory_girl/matchers/be_created_by_factory'
3
+
4
+ if defined?(RSpec)
5
+ require 'factory_girl/matchers/integrations/rspec'
6
+ else
7
+ require 'factory_girl/matchers/integrations/test_unit'
8
+ end
9
+
@@ -0,0 +1,62 @@
1
+ module FactoryGirl
2
+ module Matchers
3
+ class Base
4
+
5
+ def initialize(factory)
6
+ @factory = factory
7
+ @errors = []
8
+ end
9
+
10
+ def matches?(target)
11
+ prepare_matches(target)
12
+ return false unless @result
13
+
14
+ # record is a kind of target class
15
+ unless @result.kind_of?(@target)
16
+ @errors << "'#{@target}' class expected, but factory return object kind of #{@result.class}"
17
+ return false
18
+ end
19
+
20
+ # record is valid
21
+ unless @result.valid?
22
+ @errors += @result.errors.full_messages
23
+ return false
24
+ end
25
+
26
+ # record can be saved
27
+ begin
28
+ @result.save!
29
+ rescue ActiveRecord::RecordInvalid => e
30
+ @errors << e.to_s
31
+ return false
32
+ end
33
+
34
+ # A second record can be created
35
+ begin
36
+ ::FactoryGirl.create(@factory)
37
+ rescue ActiveRecord::RecordInvalid => e
38
+ @errors << "model has uniqueness validation, use sequence in factory (#{e.to_s})"
39
+ return false
40
+ end
41
+
42
+ @errors.empty?
43
+ end
44
+
45
+ private
46
+
47
+ def prepare_matches(target)
48
+ @target ||= target_class(target)
49
+ @factory ||= default_factory(@target)
50
+ end
51
+
52
+ def default_factory(target)
53
+ target.to_s.underscore.to_sym if target.kind_of? Class
54
+ end
55
+
56
+ def target_class(target)
57
+ target.kind_of?(Class) ? target : target.class
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,34 @@
1
+ require 'factory_girl/matchers/base'
2
+
3
+ module FactoryGirl
4
+ module Matchers
5
+
6
+ def be_built_by_factory(factory = nil)
7
+ FactoryGirl::Matchers::BeBuiltByFactory.new(factory)
8
+ end
9
+
10
+ class BeBuiltByFactory < FactoryGirl::Matchers::Base
11
+
12
+ def description
13
+ "build instance of '#{@target}' by factory '#{@factory}'"
14
+ end
15
+
16
+ def matches?(target)
17
+ prepare_matches(target)
18
+ @result = ::FactoryGirl.build(@factory)
19
+
20
+ super
21
+ end
22
+
23
+ def failure_message_for_should
24
+ "expected #{@target.inspect} to be build by factory '#{@factory}'\n" + @errors.join("\n")
25
+ end
26
+
27
+ def failure_message_for_should_not
28
+ "expected #{@target.inspect} not to be build by factory '#{@factory}'"
29
+ end
30
+
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,39 @@
1
+ require 'factory_girl/matchers/base'
2
+
3
+ module FactoryGirl
4
+ module Matchers
5
+
6
+ def be_created_by_factory(factory = nil)
7
+ FactoryGirl::Matchers::BeCreatedByFactory.new(factory)
8
+ end
9
+
10
+ class BeCreatedByFactory < FactoryGirl::Matchers::Base
11
+
12
+ def description
13
+ "create instance of '#{@target}' by factory '#{@factory}'"
14
+ end
15
+
16
+ def matches?(target)
17
+ prepare_matches(target)
18
+ begin
19
+ @result = ::FactoryGirl.create(@factory)
20
+ rescue ActiveRecord::RecordInvalid => e
21
+ @errors << e.to_s
22
+ return false
23
+ end
24
+
25
+ super
26
+ end
27
+
28
+ def failure_message_for_should
29
+ "expected #{@target.inspect} to be created by factory '#{@factory}'\n" + @errors.join("\n")
30
+ end
31
+
32
+ def failure_message_for_should_not
33
+ "expected #{@target.inspect} not to be created by factory '#{@factory}'"
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,4 @@
1
+ module RSpec::Matchers
2
+ include FactoryGirl::Matchers
3
+ end
4
+
@@ -0,0 +1,9 @@
1
+ module Test
2
+ module Unit
3
+ class TestCase
4
+ include FactoryGirl::Matchers
5
+ extend FactoryGirl::Matchers
6
+ end
7
+ end
8
+ end
9
+
@@ -0,0 +1,6 @@
1
+ module FactoryGirl
2
+ module Matchers
3
+ VERSION = '1.0.0.rc1'
4
+ end
5
+ end
6
+
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory-girl-matchers
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Yann Lugrin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70176221986220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70176221986220
25
+ - !ruby/object:Gem::Dependency
26
+ name: factory_girl
27
+ requirement: &70176221976140 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70176221976140
36
+ - !ruby/object:Gem::Dependency
37
+ name: activerecord
38
+ requirement: &70176221971520 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70176221971520
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: &70176221959100 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70176221959100
58
+ description: Making easy to validate your factories created with Factory Girl
59
+ email: yann.lugrin@sans-savoir.net
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - lib/factory-girl-matchers.rb
65
+ - lib/factory_girl/matchers/base.rb
66
+ - lib/factory_girl/matchers/be_built_by_factory.rb
67
+ - lib/factory_girl/matchers/be_created_by_factory.rb
68
+ - lib/factory_girl/matchers/integrations/rspec.rb
69
+ - lib/factory_girl/matchers/integrations/test_unit.rb
70
+ - lib/factory_girl/matchers/version.rb
71
+ - lib/factory_girl/matchers.rb
72
+ - LICENSE
73
+ - README.md
74
+ homepage: http://rubygems.org/gems/factory-girl-matchers
75
+ licenses: []
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --charset=UTF-8
79
+ - --main=README.rdoc
80
+ - --exclude='(lib|test|spec)|(Gem|Guard|Rake)file'
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ segments:
90
+ - 0
91
+ hash: 3207022547754401268
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: 1.3.6
98
+ requirements: []
99
+ rubyforge_project: factory-girl-matchers
100
+ rubygems_version: 1.8.10
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Making easy to validate your factories created with Factory Girl
104
+ test_files: []