sham 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +100 -0
- data/lib/sham/version.rb +1 -1
- data/lib/sham.rb +6 -2
- data/sham.gemspec +1 -1
- metadata +5 -5
data/README.markdown
CHANGED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Sham
|
2
|
+
|
3
|
+
Lightweight flexible factories for Ruby on Rails testing.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install sham
|
8
|
+
|
9
|
+
## Getting Started
|
10
|
+
|
11
|
+
Create a sham file for each of your models:
|
12
|
+
|
13
|
+
# in sham/user_sham.rb
|
14
|
+
class Sham::UserSham < Sham::Core
|
15
|
+
def self.options
|
16
|
+
{ :name => "Sample User" }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Note: Sham is automatically enabled in test and cucumber environments. You can manually enable it or disable
|
21
|
+
it by using the enable! and disable! commands in your environment.rb or test.rb files:
|
22
|
+
|
23
|
+
Sham::Config.enable!
|
24
|
+
Sham::Config.disable!
|
25
|
+
|
26
|
+
You can now "sham" your models and pass additional attributes at creation:
|
27
|
+
|
28
|
+
User.sham!
|
29
|
+
User.sham! :name => "New Name"
|
30
|
+
User.sham! :age => 23
|
31
|
+
|
32
|
+
You can use sham to build models without saving them as well:
|
33
|
+
|
34
|
+
user = User.sham! :build, :name => "I have not been saved"
|
35
|
+
user.save
|
36
|
+
|
37
|
+
## RSpec Example
|
38
|
+
|
39
|
+
Here is an example of testing validations on an ActiveRecord::Base class using Sham and RSpec.
|
40
|
+
|
41
|
+
# in app/models/item.rb
|
42
|
+
class Item < ActiveRecord::Base
|
43
|
+
validates_numericality_of :quantity, :greater_than => 0
|
44
|
+
end
|
45
|
+
|
46
|
+
# in sham/item_sham.rb
|
47
|
+
class Sham::ItemSham < Sham::Core
|
48
|
+
def self.options
|
49
|
+
{ :quantity => 1 }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# in spec/models/item_spec.rb
|
54
|
+
describe Item do
|
55
|
+
it "should not allow items with a negative price" do
|
56
|
+
item = Item.sham :build, :quantity => -1
|
57
|
+
item.valid?.should be_false
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should allow items with a positive quantity" do
|
61
|
+
item = Item.sham :build, :quantity => 10
|
62
|
+
item.valid?.should be_true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
## Shamming Alternatives
|
67
|
+
|
68
|
+
You can add other alternative variations to the default "sham!" functionality:
|
69
|
+
|
70
|
+
# in sham/item_sham.rb
|
71
|
+
class Sham::ItemSham < Sham::Core
|
72
|
+
def self.options
|
73
|
+
{ :weight => 1.0 }
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.large_options
|
77
|
+
{ :weight => 100.0 }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
These can be invoked using:
|
82
|
+
|
83
|
+
Item.sham_alternate! :large, :quantity => 100
|
84
|
+
Item.sham_alternate! :large, :build, :quantity => 0
|
85
|
+
|
86
|
+
## Nested Shamming
|
87
|
+
|
88
|
+
You can nest shammed models inside others:
|
89
|
+
|
90
|
+
# in sham/line_item_sham.rb
|
91
|
+
class Sham::LineItemSham < Sham::Core
|
92
|
+
def self.options
|
93
|
+
{ :item => Sham::Base.new(Item) }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
The nested shams will automatically be invoked and can be overridden during a sham:
|
98
|
+
|
99
|
+
LineItem.sham!
|
100
|
+
LineItem.sham! :item => Item.sham!(:weight => 100)
|
data/lib/sham/version.rb
CHANGED
data/lib/sham.rb
CHANGED
@@ -12,6 +12,10 @@ module Sham
|
|
12
12
|
@@enabled = true
|
13
13
|
end
|
14
14
|
|
15
|
+
def self.disable!
|
16
|
+
@@enabled = false
|
17
|
+
end
|
18
|
+
|
15
19
|
def self.enabled?
|
16
20
|
!!@@enabled
|
17
21
|
end
|
@@ -72,7 +76,7 @@ module Sham
|
|
72
76
|
|
73
77
|
def self.sham_alternate! type, *args
|
74
78
|
options = (args.extract_options! || {})
|
75
|
-
Sham.add_options! self.name, options, "
|
79
|
+
Sham.add_options! self.name, options, "#{type}_options"
|
76
80
|
klass = (options.delete(:type) || self.name).constantize
|
77
81
|
return klass.create(options) unless args[0] == :build
|
78
82
|
return klass.new(options)
|
@@ -82,7 +86,7 @@ module Sham
|
|
82
86
|
end
|
83
87
|
end
|
84
88
|
|
85
|
-
Sham::Config.enable!
|
89
|
+
Sham::Config.enable! if ["test", "cucumber"].member?(ENV['RAILS_ENV'])
|
86
90
|
|
87
91
|
if Sham::Config.enabled?
|
88
92
|
Dir["#{RAILS_ROOT}/sham/*_sham.rb"].each{ |f| require f }
|
data/sham.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Pan Thomakos"]
|
10
10
|
s.email = ["pan.thomakos@gmail.com"]
|
11
|
-
s.homepage = "http://
|
11
|
+
s.homepage = "http://github.com/panthomakos/sham"
|
12
12
|
s.summary = "sham-#{Sham::VERSION}"
|
13
13
|
s.description = %q{Flexible factories for Ruby on Rails testing.}
|
14
14
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sham
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Pan Thomakos
|
@@ -39,7 +39,7 @@ files:
|
|
39
39
|
- lib/sham/version.rb
|
40
40
|
- sham.gemspec
|
41
41
|
has_rdoc: true
|
42
|
-
homepage: http://
|
42
|
+
homepage: http://github.com/panthomakos/sham
|
43
43
|
licenses: []
|
44
44
|
|
45
45
|
post_install_message:
|
@@ -71,6 +71,6 @@ rubyforge_project: sham
|
|
71
71
|
rubygems_version: 1.3.7
|
72
72
|
signing_key:
|
73
73
|
specification_version: 3
|
74
|
-
summary: sham-0.0.
|
74
|
+
summary: sham-0.0.2
|
75
75
|
test_files: []
|
76
76
|
|