micro_factory 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +9 -0
- data/README.md +73 -0
- data/Rakefile +9 -0
- data/lib/micro_factory.rb +21 -0
- data/micro_factory.gemspec +18 -0
- data/test.rb +44 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 356b1fa9ddde54411da3469053d2bc429331a421
|
4
|
+
data.tar.gz: 59957e93aac11d2367e5d8a4eab4aa5a777dfe41
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c742b9a7eaf73e03632662dfaf6bc1975c21900b851d5bd920acf125b07451fbcdafdeeff91afc64fbfe2a9f5fa5b5502eed6978d58d916fa0c0790da85efefd
|
7
|
+
data.tar.gz: edc047e4281dbd46e93023d371a700a9a2ed18d3f88b0ba0fc0fcc2f213b091a08bd01890acceaab860e0795bf1edb8d9e62c49c181955113396318b85efda6c
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
Copyright (c) 2015 Norman Clarke and FlexMinder
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# MicroFactory
|
2
|
+
|
3
|
+
MicroFactory is a a tiny factory library for writing tests with Active Record.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'micro_factory'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install micro_factory
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
MicroFactory adds a `factory` method to the class or module you add it to.
|
24
|
+
|
25
|
+
The method accepts a class, string or symbol reprenting a class, and a hash of valid attributes for the class.
|
26
|
+
|
27
|
+
It then generates three methods you can use to build or create Active Record model instances, and access a hash of valid attributes.
|
28
|
+
|
29
|
+
The simplest way to use MicroFactory is by extending it into `ActiveSupport::TestCase`, this makes the factories you create available throughout your test suite.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class ActiveSupport::TestCase
|
33
|
+
extend FlexMinder::Factories
|
34
|
+
factory :State, name: 'Washington', code: 'WA'
|
35
|
+
end
|
36
|
+
|
37
|
+
class StateTest
|
38
|
+
test 'it should validate the state code' do
|
39
|
+
state = build_state code: 'XX'
|
40
|
+
assert_false state.valid?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
In the given example, MicroFactory would generate the following methods:
|
46
|
+
|
47
|
+
* `build_state(attributes = {})` - Make a new state instance, optionally with the given attributes merged into the defaults.
|
48
|
+
* `create_state(attributes={})` - The same as `build_state` but saves it.
|
49
|
+
* `valid_state_attributes` - Get the default attributes used by the factory methods.
|
50
|
+
|
51
|
+
If you don't like the idea of extending ActiveRecord::TestCase, you can put it in your own module to keep it isolated:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
module MyFactories
|
55
|
+
extend MicroFactory
|
56
|
+
extend self
|
57
|
+
end
|
58
|
+
|
59
|
+
MyFactories.factory :State, name: 'Washington', code: 'WA'
|
60
|
+
oregon = MyFactories.build_state code: 'OR', name: 'Oregon'
|
61
|
+
```
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
Copyright (c) 2015 Norman Clarke and FlexMinder
|
66
|
+
|
67
|
+
MIT License
|
68
|
+
|
69
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
70
|
+
|
71
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
72
|
+
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module MicroFactory
|
2
|
+
def factory(klass, attributes)
|
3
|
+
name = klass.to_s
|
4
|
+
underscored = name.gsub(/([a-z])(::)?([A-Z])/, '\1_\3').downcase
|
5
|
+
|
6
|
+
class_eval(<<-END, __FILE__, __LINE__ + 1)
|
7
|
+
def build_#{underscored}(attributes = {})
|
8
|
+
attributes = valid_#{underscored}_attributes.merge(attributes)
|
9
|
+
#{name}.new(attributes)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_#{underscored}(attributes = {})
|
13
|
+
build_#{underscored}(attributes).tap {|x| x.save!}
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid_#{underscored}_attributes
|
17
|
+
#{attributes.inspect}
|
18
|
+
end
|
19
|
+
END
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "micro_factory"
|
3
|
+
spec.version = '0.0.1'
|
4
|
+
spec.authors = ["Norman Clarke"]
|
5
|
+
spec.email = ["norman@njclarke.com"]
|
6
|
+
spec.summary = "Minimal factories for Active Record."
|
7
|
+
spec.description = "A bare-minimum factory library for Actie Record."
|
8
|
+
spec.homepage = "https://github.com/norman/micro_factory"
|
9
|
+
spec.license = "MIT"
|
10
|
+
|
11
|
+
spec.files = `git ls-files -z`.split("\x0")
|
12
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
13
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
14
|
+
spec.require_paths = ["lib"]
|
15
|
+
|
16
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
17
|
+
spec.add_development_dependency "test-unit", "~> 3.1", ">= 3.1.2"
|
18
|
+
end
|
data/test.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
gem 'test-unit'
|
2
|
+
require 'test/unit'
|
3
|
+
require_relative 'lib/micro_factory'
|
4
|
+
|
5
|
+
class Stooge
|
6
|
+
attr_accessor :name, :occupation, :saved
|
7
|
+
|
8
|
+
def initialize(attributes)
|
9
|
+
@saved = false
|
10
|
+
attributes.each {|k, v| send("#{k}=", v)}
|
11
|
+
end
|
12
|
+
|
13
|
+
def save!
|
14
|
+
@saved = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class MicroFactoryTest < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@mod = Module.new
|
21
|
+
@mod.extend MicroFactory
|
22
|
+
@mod.extend @mod
|
23
|
+
@mod.factory :Stooge, name: 'Moe Howard', occupation: 'Actor'
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_adds_methods_to_module
|
27
|
+
assert_equal [:build_stooge, :create_stooge, :valid_stooge_attributes], @mod.public_instance_methods
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_valid_attributes
|
31
|
+
assert_equal({name: 'Moe Howard', occupation: 'Actor'}, @mod.valid_stooge_attributes)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_build
|
35
|
+
stooge = @mod.build_stooge name: 'Larry Fine'
|
36
|
+
assert_equal 'Larry Fine', stooge.name
|
37
|
+
assert_equal 'Actor', stooge.occupation
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_create
|
41
|
+
stooge = @mod.create_stooge
|
42
|
+
assert stooge.saved
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: micro_factory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Norman Clarke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: test-unit
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 3.1.2
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.1'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.1.2
|
47
|
+
description: A bare-minimum factory library for Actie Record.
|
48
|
+
email:
|
49
|
+
- norman@njclarke.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- LICENSE.txt
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- lib/micro_factory.rb
|
58
|
+
- micro_factory.gemspec
|
59
|
+
- test.rb
|
60
|
+
homepage: https://github.com/norman/micro_factory
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.7
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Minimal factories for Active Record.
|
84
|
+
test_files: []
|
85
|
+
has_rdoc:
|