shoulda_create 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +22 -0
- data/lib/shoulda_create.rb +3 -0
- data/lib/shoulda_create/shoulda_create.rb +84 -0
- data/lib/shoulda_create/test_unit.rb +3 -0
- metadata +68 -0
data/README.rdoc
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
== shoulda_create
|
2
|
+
|
3
|
+
Restores should_create functionality to shoulda.
|
4
|
+
|
5
|
+
I like using should_create and friends to make assertions about records coming and going in the database.
|
6
|
+
|
7
|
+
Shoulda did, but doesn't any more. With this, it can again.
|
8
|
+
|
9
|
+
Some ideological dispute, I guess. I complained to one of the thoughtbot guys, and he said "that's the joy of open source, you can build your own". Well, here it is.
|
10
|
+
|
11
|
+
== Example
|
12
|
+
|
13
|
+
context 'POST to :create' do
|
14
|
+
setup { post :create, :email => 'bob@example.com', :name => 'Bob Dobbs' }
|
15
|
+
should_create :user
|
16
|
+
end
|
17
|
+
|
18
|
+
== License
|
19
|
+
|
20
|
+
shoulda_create is released under the MIT license:
|
21
|
+
|
22
|
+
* http://www.opensource.org/licenses/MIT
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module ShouldaCreate
|
2
|
+
def should_change_record_count_of(class_name, amount, action) # :nodoc:
|
3
|
+
klass = class_name.to_s.camelize.constantize
|
4
|
+
var_name = "@_before_change_record_count_#{class_name}"
|
5
|
+
before = lambda do
|
6
|
+
instance_variable_set var_name, klass.count
|
7
|
+
end
|
8
|
+
human_name = class_name.to_s.humanize.downcase
|
9
|
+
count = 'a'
|
10
|
+
if amount > 1
|
11
|
+
human_name = human_name.pluralize
|
12
|
+
count = amount
|
13
|
+
end
|
14
|
+
should "#{action} #{count} #{human_name}", :before => before do
|
15
|
+
assert_equal instance_variable_get(var_name) + amount,
|
16
|
+
klass.count,
|
17
|
+
"Expected to #{action} a #{human_name}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def should_not_change_record_count_of(class_name) # :nodoc:
|
22
|
+
should_change_record_count_of class_name, 0, 'neither create nor destroy'
|
23
|
+
end
|
24
|
+
|
25
|
+
def should_not_create class_name
|
26
|
+
should_change_record_count_of(class_name, 0, 'create')
|
27
|
+
end
|
28
|
+
|
29
|
+
def should_create(class_name, options = {})
|
30
|
+
count = options[:count] || 1
|
31
|
+
should_change_record_count_of(class_name, count, 'create')
|
32
|
+
end
|
33
|
+
|
34
|
+
def should_destroy(class_name, options = {})
|
35
|
+
count = options[:count] || -1
|
36
|
+
should_change_record_count_of(class_name, count, 'destroy')
|
37
|
+
end
|
38
|
+
|
39
|
+
def should_change(description, options = {}, &block)
|
40
|
+
by, from, to = get_options!([options], :by, :from, :to)
|
41
|
+
stmt = "change #{description}"
|
42
|
+
stmt << " from #{from.inspect}" if from
|
43
|
+
stmt << " to #{to.inspect}" if to
|
44
|
+
stmt << " by #{by.inspect}" if by
|
45
|
+
|
46
|
+
if block_given?
|
47
|
+
code = block
|
48
|
+
else
|
49
|
+
warn "[DEPRECATION] should_change(expression, options) is deprecated. " <<
|
50
|
+
"Use should_change(description, options) { code } instead."
|
51
|
+
code = lambda { eval(description) }
|
52
|
+
end
|
53
|
+
|
54
|
+
before_var_name = '@_before_should_change_' + description.downcase.gsub(/[^\w]/, '_')
|
55
|
+
before = lambda { self.instance_variable_set( before_var_name, code.bind(self).call ) }
|
56
|
+
should stmt, :before => before do
|
57
|
+
old_value = self.instance_variable_get( before_var_name )
|
58
|
+
new_value = code.bind(self).call
|
59
|
+
assert_operator from, :===, old_value, "#{description} did not originally match #{from.inspect}" if from
|
60
|
+
assert_not_equal old_value, new_value, "#{description} did not change" unless by == 0
|
61
|
+
assert_operator to, :===, new_value, "#{description} was not changed to match #{to.inspect}" if to
|
62
|
+
assert_equal old_value + by, new_value if by
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def should_not_change(description, &block)
|
67
|
+
|
68
|
+
if block_given?
|
69
|
+
code = block
|
70
|
+
else
|
71
|
+
warn "[DEPRECATION] should_change(expression, options) is deprecated. " <<
|
72
|
+
"Use should_change(description, options) { code } instead."
|
73
|
+
code = lambda { eval(description) }
|
74
|
+
end
|
75
|
+
|
76
|
+
before_var_name = '@_before_should_change_' + description.downcase.gsub(/[^\w]/, '_')
|
77
|
+
before = lambda { self.instance_variable_set( before_var_name, code.bind(self).call ) }
|
78
|
+
should "not change #{description}", :before => before do
|
79
|
+
old_value = self.instance_variable_get( before_var_name )
|
80
|
+
new_value = code.bind(self).call
|
81
|
+
assert_equal old_value, new_value, "#{description} changed"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shoulda_create
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Bill Kirtley
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-02-16 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: I like using should_create and friends to make assertions about records coming and going in the database. Shoulda did, but doesn't any more. With this, it can again.
|
22
|
+
email: bill.kirtley@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/shoulda_create/shoulda_create.rb
|
31
|
+
- lib/shoulda_create/test_unit.rb
|
32
|
+
- lib/shoulda_create.rb
|
33
|
+
- README.rdoc
|
34
|
+
homepage: http://github.com/cluesque/shoulda_create
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.15
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Restores should_create functionality to shoulda.
|
67
|
+
test_files: []
|
68
|
+
|