sham 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1 +1,3 @@
1
1
  source "http://rubygems.org"
2
+
3
+ gemspec
data/License.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Panayiotis Thomakos
1
+ Copyright (c) 2011 Panayiotis Thomakos
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.markdown CHANGED
@@ -10,96 +10,155 @@ Lightweight flexible factories for Ruby on Rails testing.
10
10
 
11
11
  Create a sham file for each of your models:
12
12
 
13
- # in sham/user_sham.rb
14
- class User::Sham
15
- def self.options
16
- { :name => "Sample User" }
17
- end
13
+ # in sham/user.rb
14
+ Sham.config(User) do |c|
15
+ c.attributes do
16
+ { :name => "Sample User" }
17
+ end
18
18
  end
19
19
 
20
- To enable Sham in a particular environment, add the following to your environment.rb or test.rb file:
20
+ To load your shams you can either include the files individually, or define
21
+ your shams directly in your test file. Sham also provides a helper function to
22
+ load shams under the sham directory. To load all your shams add the following to
23
+ your application.rb or test.rb file:
21
24
 
22
25
  config.after_initialize do
23
- Sham::Config.activate!
26
+ Sham::Config.activate!
24
27
  end
25
-
26
- To enable Sham in cucumber, add the following to your features/support/env.rb file:
28
+
29
+ To enable all Shams in cucumber, add the following to your
30
+ features/support/env.rb file:
27
31
 
28
32
  require 'sham'
29
- Sham::Config.activate!
33
+ Sham::Config.activate!
30
34
 
31
35
  You can now "sham" your models and pass additional attributes at creation:
32
36
 
33
37
  User.sham!
34
- User.sham! :name => "New Name"
35
- User.sham! :age => 23
36
-
37
- You can use sham to build models without saving them as well:
38
+ User.sham!(:name => "New Name")
39
+ User.sham!(:age => 23)
40
+
41
+ You can use sham to build models without automatically saving them as well:
38
42
 
39
- user = User.sham! :build, :name => "I have not been saved"
43
+ user = User.sham!(:build, :name => "I have not been saved")
40
44
  user.save
41
-
45
+
42
46
  ## RSpec Example
43
47
 
44
- Here is an example of testing validations on an ActiveRecord::Base class using Sham and RSpec.
48
+ Here is an example of testing validations on an ActiveRecord::Base class using
49
+ Sham and RSpec.
45
50
 
46
51
  # in app/models/item.rb
47
52
  class Item < ActiveRecord::Base
48
- validates_numericality_of :quantity, :greater_than => 0
53
+ validates_numericality_of :quantity, :greater_than => 0
49
54
  end
50
55
 
51
- # in sham/item_sham.rb
52
- class Item::Sham
53
- def self.options
54
- { :quantity => 1 }
55
- end
56
+ # in sham/item.rb
57
+ Sham.config(Item) do |c|
58
+ c.attributes do
59
+ { :quantity => 1 }
60
+ end
56
61
  end
57
62
 
58
63
  # in spec/models/item_spec.rb
64
+ require 'spec_helper'
65
+ require './sham/item'
66
+
59
67
  describe Item do
60
- it "should not allow items with a negative price" do
61
- item = Item.sham! :build, :quantity => -1
62
- item.valid?.should be_false
63
- end
64
-
65
- it "should allow items with a positive quantity" do
66
- item = Item.sham! :build, :quantity => 10
67
- item.valid?.should be_true
68
- end
68
+ it "should not allow items with a negative price" do
69
+ item = Item.sham!(:build, :quantity => -1)
70
+ item.valid?.should be_false
71
+ end
72
+
73
+ it "should allow items with a positive quantity" do
74
+ item = Item.sham!(:build, :quantity => 10)
75
+ item.valid?.should be_true
76
+ end
69
77
  end
70
-
71
- ## Shamming Alternatives
72
-
73
- You can add other alternative variations to the default "sham!" functionality:
74
-
75
- # in sham/item_sham.rb
76
- class Item::Sham
77
- def self.options
78
- { :weight => 1.0 }
79
- end
80
-
81
- def self.large_options
82
- { :weight => 100.0 }
83
- end
78
+
79
+ ## Alternative Shams
80
+
81
+ You can easily define alternative sham configurations:
82
+
83
+ # in sham/item.rb
84
+ Sham.config(Item, :small) do |c|
85
+ c.attributes do
86
+ { :weight => 10.0 }
87
+ end
84
88
  end
85
-
89
+
90
+ Sham.config(Item, :large) do |c|
91
+ c.attributes do
92
+ { :weight => 100.0 }
93
+ end
94
+ end
95
+
86
96
  These can be invoked using:
87
97
 
88
- Item.sham_alternate! :large, :quantity => 100
89
- Item.sham_alternate! :large, :build, :quantity => 0
90
-
98
+ Item.sham!(:small, :quantity => 100)
99
+ Item.sham!(:large, :build, :quantity => 0)
100
+
101
+ ## Empty Shams
102
+
103
+ You can easily define empty shams using the empty function:
104
+
105
+ # in sham/user.rb
106
+ Sham.config(User) do |c|
107
+ c.empty
108
+ end
109
+
110
+ This can be invoked using:
111
+
112
+ User.sham!
113
+
91
114
  ## Nested Shamming
92
115
 
93
116
  You can nest shammed models inside others:
94
117
 
95
118
  # in sham/line_item_sham.rb
96
- class LineItem::Sham
97
- def self.options
98
- { :item => Sham::Base.new(Item) }
99
- end
119
+ Sham.config(LineItem) do |c|
120
+ c.attributes do
121
+ { :item => Sham::Base.new(Item) }
122
+ end
100
123
  end
101
124
 
102
- The nested shams will automatically be invoked and can be overridden during a sham:
125
+ The nested shams will automatically be invoked and can be overridden during a
126
+ sham call:
103
127
 
104
128
  LineItem.sham!
105
- LineItem.sham! :item => Item.sham!(:weight => 100)
129
+ LineItem.sham!(:item => Item.sham!(:weight => 100))
130
+
131
+
132
+ ## Subclass Shams
133
+
134
+ Sham plays well with subclassing. That means shams defined on parent classes
135
+ will be available to child classes as well:
136
+
137
+ Sham.config(Person) do |c|
138
+ c.empty
139
+ end
140
+
141
+ class Person; end
142
+ class Employee < Person; end
143
+
144
+ Employee.sham!
145
+
146
+ ## Reloading Shams with Spork
147
+
148
+ [Spork](https://rubygems.org/gems/spork) is a great gem that creates a
149
+ Distributed Ruby environment that you can run your RSpec and Cucumber tests
150
+ against. If you are using Rails it is often necessary to re-load your models and
151
+ controllers between Spork test runs so that the Spork DRB picks up your latest
152
+ model changes. This is usually accomplished using a Spork 'each run' block. This
153
+ block of code gets executed before each test run. If you want to be able to
154
+ reload your shams with Spork all you need to do is add a Sham::Config.activate!
155
+ line to this block after you have re-loaded your models and controllers.
156
+
157
+ Spork.each_run do
158
+ ActiveSupport::Dependencies.clear
159
+ ActiveRecord::Base.instantiate_observers
160
+ Sham::Config.activate!
161
+ end if Spork.using_spork?
162
+
163
+ This change will cause sham to be re-loaded so that you can continue to use it
164
+ with Spork.
data/lib/sham.rb CHANGED
@@ -14,11 +14,32 @@ module Sham
14
14
 
15
15
  class Config
16
16
  def self.activate!
17
- Dir["#{Rails.root}/sham/*_sham.rb"].each do |f|
17
+ Dir["#{Rails.root}/sham/**/*.rb"].each do |f|
18
18
  load f
19
- (File.basename(f).match(/(.*)_sham.rb/)[1]).classify.constantize.send :include, Sham::Methods
20
19
  end
21
20
  end
21
+
22
+ attr_accessor :klass, :name
23
+
24
+ def initialize klass, name
25
+ self.klass = klass
26
+ self.name = name
27
+ end
28
+
29
+ def attributes &attributes
30
+ klass.add_sham_attributes(name, attributes)
31
+ end
32
+
33
+ def empty
34
+ klass.add_sham_attributes(name, Proc.new{ Hash.new() })
35
+ end
36
+ end
37
+
38
+ def self.config klass, name = :default
39
+ unless klass.include?(Sham::Shammable)
40
+ klass.send(:include, Sham::Shammable)
41
+ end
42
+ yield(Sham::Config.new(klass, name))
22
43
  end
23
44
 
24
45
  def self.string!
@@ -37,31 +58,46 @@ module Sham
37
58
  end
38
59
  end
39
60
 
40
- def self.add_options! klass, options = {}, options_string = "options"
41
- eval("#{klass}::Sham.#{options_string}").each do |key, value|
61
+ def self.add_options! klass, options = {}, attributes
62
+ attributes.call.each do |key, value|
42
63
  options[key] = self.parse!(value) unless options.has_key?(key)
43
64
  end
44
65
  end
45
66
 
46
- module Methods
47
- def self.included klass
48
- klass.class_eval do
49
- def self.sham! *args
50
- options = (args.extract_options! || {})
51
- ::Sham.add_options! self.name, options
52
- klass = (options.delete(:type) || self.name).constantize
53
- return klass.create(options) unless args[0] == :build
54
- return klass.new(options)
67
+ module Shammable
68
+ def self.included(klass)
69
+ klass.extend(ClassMethods)
70
+
71
+ klass.class_eval <<-EVAL
72
+ def self.add_sham_attributes name, attributes
73
+ @@sham_attributes ||= {}
74
+ @@sham_attributes[name] = attributes
75
+ end
76
+
77
+ def self.sham_attributes
78
+ @@sham_attributes
55
79
  end
80
+ EVAL
81
+ end
82
+
83
+ module ClassMethods
84
+ def sham! *args
85
+ options = (args.extract_options! || {})
86
+ type = (args[0] == :build ? args[1] : args[0]) || :default
87
+ build = args[0] == :build || args[1] == :build
56
88
 
57
- def self.sham_alternate! type, *args
58
- options = (args.extract_options! || {})
59
- ::Sham.add_options! self.name, options, "#{type}_options"
60
- klass = (options.delete(:type) || self.name).constantize
61
- return klass.create(options) unless args[0] == :build
62
- return klass.new(options)
89
+ ::Sham.add_options! self.name, options, sham_attributes[type]
90
+ klass = (options.delete(:type) || self.name).constantize
91
+ if build
92
+ klass.new(options)
93
+ else
94
+ klass.create(options)
63
95
  end
64
96
  end
97
+
98
+ def sham_alternate! type, *args
99
+ sham!(type, *args)
100
+ end
65
101
  end
66
102
  end
67
103
  end
data/lib/sham/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sham
2
- VERSION = "0.5.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/sham.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.extra_rdoc_files = ["README.markdown", "License.txt"]
21
21
  s.rdoc_options = ["--charset=UTF-8"]
22
22
  s.require_paths = ["lib"]
23
+ s.add_development_dependency('rails', '>= 3.0.1')
23
24
  end
metadata CHANGED
@@ -1,30 +1,36 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sham
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
4
5
  prerelease:
5
- version: 0.5.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Pan Thomakos
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-05-18 00:00:00 -07:00
14
- default_executable:
15
- dependencies: []
16
-
12
+ date: 2011-09-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70123949484060 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.1
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70123949484060
17
25
  description: Lightweight flexible factories for Ruby on Rails testing.
18
- email:
26
+ email:
19
27
  - pan.thomakos@gmail.com
20
28
  executables: []
21
-
22
29
  extensions: []
23
-
24
- extra_rdoc_files:
30
+ extra_rdoc_files:
25
31
  - README.markdown
26
32
  - License.txt
27
- files:
33
+ files:
28
34
  - .document
29
35
  - .gitignore
30
36
  - Gemfile
@@ -34,33 +40,29 @@ files:
34
40
  - lib/sham.rb
35
41
  - lib/sham/version.rb
36
42
  - sham.gemspec
37
- has_rdoc: true
38
43
  homepage: http://github.com/panthomakos/sham
39
44
  licenses: []
40
-
41
45
  post_install_message:
42
- rdoc_options:
46
+ rdoc_options:
43
47
  - --charset=UTF-8
44
- require_paths:
48
+ require_paths:
45
49
  - lib
46
- required_ruby_version: !ruby/object:Gem::Requirement
50
+ required_ruby_version: !ruby/object:Gem::Requirement
47
51
  none: false
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- version: "0"
52
- required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
57
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
58
62
  requirements: []
59
-
60
63
  rubyforge_project: sham
61
- rubygems_version: 1.6.0
64
+ rubygems_version: 1.8.6
62
65
  signing_key:
63
66
  specification_version: 3
64
- summary: sham-0.5.0
67
+ summary: sham-1.0.0
65
68
  test_files: []
66
-