riot_rails 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  [Riot](http://github.com/thumblemonks/riot) macros for Rails application testing.
4
4
 
5
- More to come ...
5
+ LOTS more to come ...
6
6
 
7
7
  ### ActiveRecord
8
8
 
9
9
  Standard macros
10
10
 
11
- * should\_validate\_presence\_of
12
- * should\_allow\_values\_for
13
- * should\_not\_allow\_values\_for
14
- * should\_validate\_uniquness\_of
11
+ * #validates\_presence\_of
12
+ * #allows\_values\_for
13
+ * #does\_not\_allow\_values\_for
14
+ * #validates\_uniquness\_of
15
15
 
16
16
  Yes. Replacing Shoulda macros for now. Like I said with Riot, I love Shoulda.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -0,0 +1,104 @@
1
+ module Riot
2
+ module ActiveRecord
3
+
4
+ module AssertionMacros
5
+
6
+ # An ActiveRecord assertion that expects to fail when a given attribute is validated after a nil value
7
+ # is provided to it.
8
+ #
9
+ # context "a User" do
10
+ # setup { User.new }
11
+ # topic.validates_presence_of(:name)
12
+ # end
13
+ def validates_presence_of(attribute)
14
+ msg = "expected to validate presence of #{attribute.inspect}"
15
+ error_from_writing_value(actual, attribute, nil) || fail(msg)
16
+ end
17
+
18
+ # An ActiveRecord assertion that expects to pass with a given value or set of values for a given
19
+ # attribute.
20
+ #
21
+ # context "a User" do
22
+ # setup { User.new }
23
+ # topic.allows_values_for :email, "a@b.cd"
24
+ # topic.allows_values_for :email, "a@b.cd", "e@f.gh"
25
+ # end
26
+ def allows_values_for(attribute, *values)
27
+ bad_values = []
28
+ values.each do |value|
29
+ bad_values << value if error_from_writing_value(actual, attribute, value)
30
+ end
31
+ msg = "expected #{attribute.inspect} to allow value(s) #{bad_values.inspect}"
32
+ fail(msg) unless bad_values.empty?
33
+ end
34
+
35
+ # An ActiveRecord assertion that expects to fail with a given value or set of values for a given
36
+ # attribute.
37
+ #
38
+ # context "a User" do
39
+ # setup { User.new }
40
+ # topic.does_not_allow_values_for :email, "a"
41
+ # topic.does_not_allow_values_for :email, "a@b", "e f@g.h"
42
+ # end
43
+ def does_not_allow_values_for(attribute, *values)
44
+ good_values = []
45
+ values.each do |value|
46
+ good_values << value unless error_from_writing_value(actual, attribute, value)
47
+ end
48
+ msg = "expected #{attribute.inspect} not to allow value(s) #{good_values.inspect}"
49
+ fail(msg) unless good_values.empty?
50
+ end
51
+
52
+ # An ActiveRecord assertion that expects to fail with an attribute is not valid for record because the
53
+ # value of the attribute is not unique. Requires the topic of the context to be a created record; one
54
+ # that returns false for a call to +new_record?+.
55
+ #
56
+ # context "a User" do
57
+ # setup { User.create(:email => "a@b.cde", ... ) }
58
+ # topic.validates_uniqueness_of :email
59
+ # end
60
+ def validates_uniqueness_of(attribute)
61
+ actual_record = actual
62
+ if actual_record.new_record?
63
+ fail("topic is not a new record when testing uniqueness of #{attribute}")
64
+ else
65
+ copied_model = actual_record.class.new
66
+ copied_value = actual_record.read_attribute(attribute)
67
+ msg = "expected to fail because #{attribute.inspect} is not unique"
68
+ error_from_writing_value(copied_model, attribute, copied_value) || fail(msg)
69
+ end
70
+ end
71
+
72
+ # An ActiveRecord assertion macro that expects to pass when a given attribute is defined as +has_many+
73
+ # association. Will fail if an association is not defined for the attribute and if the association is
74
+ # not +has_many.
75
+ #
76
+ # context "a Room" do
77
+ # setup { Room.new }
78
+ #
79
+ # topic.has_many(:doors)
80
+ # topic.has_many(:floors) # should probably fail given our current universe :)
81
+ # end
82
+ def has_many(attribute)
83
+ reflection = actual.class.reflect_on_association(attribute)
84
+ static_msg = "expected #{attribute.inspect} to be a has_many association, but was "
85
+ if reflection.nil?
86
+ fail(static_msg + "not")
87
+ elsif "has_many" != reflection.macro.to_s
88
+ fail(static_msg + "a #{reflection.macro} instead")
89
+ end
90
+ end
91
+ private
92
+
93
+ def error_from_writing_value(model, attribute, value)
94
+ model.write_attribute(attribute, value)
95
+ model.valid?
96
+ model.errors.on(attribute)
97
+ end
98
+
99
+ end # AssertionMacros
100
+
101
+ end # ActiveRecord
102
+ end # Riot
103
+
104
+ Riot::Assertion.instance_eval { include Riot::ActiveRecord::AssertionMacros }
@@ -1,2 +1 @@
1
- require 'riot/active_record/context_macros'
2
- require 'riot/active_record/situation_macros'
1
+ require 'riot/active_record/assertion_macros'
data/riot_rails.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{riot_rails}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Justin 'Gus' Knowlden"]
12
- s.date = %q{2009-10-16}
12
+ s.date = %q{2009-10-18}
13
13
  s.description = %q{Riot specific test support for Rails apps. Protest the slow app.}
14
14
  s.email = %q{gus@gusg.us}
15
15
  s.extra_rdoc_files = [
@@ -26,8 +26,7 @@ Gem::Specification.new do |s|
26
26
  "lib/riot/action_controller/context_macros.rb",
27
27
  "lib/riot/action_controller/situation_macros.rb",
28
28
  "lib/riot/active_record.rb",
29
- "lib/riot/active_record/context_macros.rb",
30
- "lib/riot/active_record/situation_macros.rb",
29
+ "lib/riot/active_record/assertion_macros.rb",
31
30
  "lib/riot/rails.rb",
32
31
  "rails/init.rb",
33
32
  "riot_rails.gemspec",
@@ -37,9 +36,10 @@ Gem::Specification.new do |s|
37
36
  "test/action_controller/renders_template_test.rb",
38
37
  "test/action_controller/renders_test.rb",
39
38
  "test/action_controller/response_status_test.rb",
40
- "test/active_record/should_allow_values_for_test.rb",
41
- "test/active_record/should_validate_presence_of_test.rb",
42
- "test/active_record/should_validate_uniqueness_of_test.rb",
39
+ "test/active_record/allowing_values_test.rb",
40
+ "test/active_record/has_many_test.rb",
41
+ "test/active_record/validates_presence_of_test.rb",
42
+ "test/active_record/validates_uniqueness_of_test.rb",
43
43
  "test/rails_root/app/views/rendered_templates/foo_bar.html.erb",
44
44
  "test/rails_root/config/routes.rb",
45
45
  "test/rails_root/db/schema.rb",
@@ -57,9 +57,10 @@ Gem::Specification.new do |s|
57
57
  "test/action_controller/renders_template_test.rb",
58
58
  "test/action_controller/renders_test.rb",
59
59
  "test/action_controller/response_status_test.rb",
60
- "test/active_record/should_allow_values_for_test.rb",
61
- "test/active_record/should_validate_presence_of_test.rb",
62
- "test/active_record/should_validate_uniqueness_of_test.rb",
60
+ "test/active_record/allowing_values_test.rb",
61
+ "test/active_record/has_many_test.rb",
62
+ "test/active_record/validates_presence_of_test.rb",
63
+ "test/active_record/validates_uniqueness_of_test.rb",
63
64
  "test/rails_root/config/routes.rb",
64
65
  "test/rails_root/db/schema.rb",
65
66
  "test/teststrap.rb"
@@ -1,36 +1,36 @@
1
1
  require 'teststrap'
2
2
 
3
- context "should_allow_values_for" do
3
+ context "allows_values_for" do
4
4
  setup_and_run_context("when attribute allows a value", 1, 0, 0) do |test_ctx|
5
5
  test_ctx.setup { Room.new }
6
- test_ctx.should_allow_values_for :email, "a@b.cd"
6
+ test_ctx.topic.allows_values_for :email, "a@b.cd"
7
7
  end
8
8
 
9
- setup_and_run_context("when attribute allows multiple values", 2, 0, 0) do |test_ctx|
9
+ setup_and_run_context("when attribute allows multiple values", 1, 0, 0) do |test_ctx|
10
10
  test_ctx.setup { Room.new }
11
- test_ctx.should_allow_values_for :email, "a@b.cd", "e@f.gh"
11
+ test_ctx.topic.allows_values_for :email, "a@b.cd", "e@f.gh"
12
12
  end
13
13
 
14
- setup_and_run_context("when attribute is provided a valid and an invalid value", 1, 1, 0) do |test_ctx|
14
+ setup_and_run_context("when attribute is provided a valid and an invalid value", 0, 1, 0) do |test_ctx|
15
15
  test_ctx.setup { Room.new }
16
- test_ctx.should_allow_values_for :email, "a", "e@f.gh"
16
+ test_ctx.topic.allows_values_for :email, "a", "e@f.gh"
17
17
  end
18
- end # should_allow_values_for
18
+ end # allows_values_for
19
19
 
20
- context "should_not_allow_values_for" do
20
+ context "does_not_allow_values_for" do
21
21
  setup_and_run_context("when attribute does not allow a value", 1, 0, 0) do |test_ctx|
22
22
  test_ctx.setup { Room.new }
23
- test_ctx.should_not_allow_values_for :email, "a"
23
+ test_ctx.topic.does_not_allow_values_for :email, "a"
24
24
  test_ctx.assertions.each {|a| STDOUT.puts a.raised if a.errored?}
25
25
  end
26
26
 
27
- setup_and_run_context("when attribute does not allow multiple values", 2, 0, 0) do |test_ctx|
27
+ setup_and_run_context("when attribute does not allow multiple values", 1, 0, 0) do |test_ctx|
28
28
  test_ctx.setup { Room.new }
29
- test_ctx.should_not_allow_values_for :email, "a", "e"
29
+ test_ctx.topic.does_not_allow_values_for :email, "a", "e"
30
30
  end
31
31
 
32
- setup_and_run_context("when attribute is provided a valid and an invalid value", 1, 1, 0) do |test_ctx|
32
+ setup_and_run_context("when attribute is provided a valid and an invalid value", 0, 1, 0) do |test_ctx|
33
33
  test_ctx.setup { Room.new }
34
- test_ctx.should_not_allow_values_for :email, "a", "e@f.gh"
34
+ test_ctx.topic.does_not_allow_values_for :email, "a", "e@f.gh"
35
35
  end
36
- end # should_not_allow_values_for
36
+ end # does_not_allow_values_for
@@ -0,0 +1,46 @@
1
+ require 'teststrap'
2
+
3
+ context "testing has_many" do
4
+
5
+ setup do
6
+ context = Riot::Context.new("has_many test context", Riot::NilReport.new)
7
+ context.setup { Room.new }
8
+ context.topic
9
+ end
10
+
11
+ context "when record has a has_many association defined for attribute" do
12
+ setup do
13
+ topic.has_many(:doors)
14
+ topic
15
+ end
16
+
17
+ asserts("assertion passed") { topic.passed? }
18
+ end # when record has a has_many association defined for attribute
19
+
20
+ context "when record does not have a has_many association defined for attribute" do
21
+ setup do
22
+ topic.has_many(:windows)
23
+ topic
24
+ end
25
+
26
+ asserts("assertion failed") { topic.failed? }
27
+
28
+ asserts("failure message") do
29
+ topic.result.message
30
+ end.matches(/expected :windows to be a has_many association, but was not/)
31
+ end # when record does not have a has_many association defined for attribute
32
+
33
+ context "when attribute is not a has_many, but is a has_one association" do
34
+ setup do
35
+ topic.has_many(:floor)
36
+ topic
37
+ end
38
+
39
+ asserts("assertion failed") { topic.failed? }
40
+
41
+ asserts("failure message") do
42
+ topic.result.message
43
+ end.matches(/expected :floor to be a has_many association, but was a has_one instead/)
44
+ end # when attribute is not a has_many, but is a has_one association
45
+
46
+ end # testing has_many
@@ -0,0 +1,13 @@
1
+ require 'teststrap'
2
+
3
+ context "validates_presence_of" do
4
+ setup_and_run_context("when attribute requires presence", 1, 0, 0) do |test_ctx|
5
+ test_ctx.setup { Room.new }
6
+ test_ctx.topic.validates_presence_of(:location)
7
+ end
8
+
9
+ setup_and_run_context("when attribute does not require presence", 0, 1, 0) do |test_ctx|
10
+ test_ctx.setup { Room.new }
11
+ test_ctx.topic.validates_presence_of(:contents)
12
+ end
13
+ end # validates_presence_of
@@ -0,0 +1,20 @@
1
+ require 'teststrap'
2
+
3
+ context "validates_uniqueness_of" do
4
+
5
+ setup_and_run_context("without a persisted record", 0, 1, 0) do |test_ctx|
6
+ test_ctx.setup { Room.new(:email => "foo@bar.baz") }
7
+ test_ctx.topic.validates_uniqueness_of :email
8
+ end
9
+
10
+ setup_and_run_context("with a persisted record", 1, 0, 0) do |test_ctx|
11
+ test_ctx.setup { Room.create_with_good_data(:email => "foo@bar.baz") }
12
+ test_ctx.topic.validates_uniqueness_of :email
13
+ end
14
+
15
+ setup_and_run_context("with a persisted record but not validating uniqueness", 0, 1, 0) do |test_ctx|
16
+ test_ctx.setup { Room.create_with_good_data(:email => "goo@car.caz") }
17
+ test_ctx.topic.validates_uniqueness_of :foo
18
+ end
19
+
20
+ end # validates_uniqueness_of
data/test/teststrap.rb CHANGED
@@ -32,6 +32,9 @@ class Room < ActiveRecord::Base
32
32
  validates_format_of :email, :with => /^\w+@\w+\.\w+$/
33
33
  validates_uniqueness_of :email
34
34
 
35
+ has_many :doors
36
+ has_one :floor
37
+
35
38
  def self.create_with_good_data(attributes={})
36
39
  create!({:location => "a", :foo => "b", :bar => "c", :email => "a@b.c"}.merge(attributes))
37
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riot_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin 'Gus' Knowlden
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-16 00:00:00 -05:00
12
+ date: 2009-10-18 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -51,8 +51,7 @@ files:
51
51
  - lib/riot/action_controller/context_macros.rb
52
52
  - lib/riot/action_controller/situation_macros.rb
53
53
  - lib/riot/active_record.rb
54
- - lib/riot/active_record/context_macros.rb
55
- - lib/riot/active_record/situation_macros.rb
54
+ - lib/riot/active_record/assertion_macros.rb
56
55
  - lib/riot/rails.rb
57
56
  - rails/init.rb
58
57
  - riot_rails.gemspec
@@ -62,9 +61,10 @@ files:
62
61
  - test/action_controller/renders_template_test.rb
63
62
  - test/action_controller/renders_test.rb
64
63
  - test/action_controller/response_status_test.rb
65
- - test/active_record/should_allow_values_for_test.rb
66
- - test/active_record/should_validate_presence_of_test.rb
67
- - test/active_record/should_validate_uniqueness_of_test.rb
64
+ - test/active_record/allowing_values_test.rb
65
+ - test/active_record/has_many_test.rb
66
+ - test/active_record/validates_presence_of_test.rb
67
+ - test/active_record/validates_uniqueness_of_test.rb
68
68
  - test/rails_root/app/views/rendered_templates/foo_bar.html.erb
69
69
  - test/rails_root/config/routes.rb
70
70
  - test/rails_root/db/schema.rb
@@ -104,9 +104,10 @@ test_files:
104
104
  - test/action_controller/renders_template_test.rb
105
105
  - test/action_controller/renders_test.rb
106
106
  - test/action_controller/response_status_test.rb
107
- - test/active_record/should_allow_values_for_test.rb
108
- - test/active_record/should_validate_presence_of_test.rb
109
- - test/active_record/should_validate_uniqueness_of_test.rb
107
+ - test/active_record/allowing_values_test.rb
108
+ - test/active_record/has_many_test.rb
109
+ - test/active_record/validates_presence_of_test.rb
110
+ - test/active_record/validates_uniqueness_of_test.rb
110
111
  - test/rails_root/config/routes.rb
111
112
  - test/rails_root/db/schema.rb
112
113
  - test/teststrap.rb
@@ -1,69 +0,0 @@
1
- module Riot
2
- module ActiveRecord
3
-
4
- module ContextMacros
5
- # An ActiveRecord assertion that expects to fail when a given attribute or attributes are validated
6
- # when a nil value is provided to them.
7
- #
8
- # Example
9
- # should_validate_presence_of :name
10
- # should_validate_presence_of :name, :email
11
- def should_validate_presence_of(*attributes)
12
- attributes.each do |attribute|
13
- should("require value for #{attribute}") do
14
- get_error_from_writing_value(topic, attribute, nil)
15
- end.exists
16
- end
17
- end
18
-
19
- # An ActiveRecord assertion that expects to pass with a given value or set of values for a given
20
- # attribute.
21
- #
22
- # Example
23
- # should_allow_values_for :email, "a@b.cd"
24
- # should_allow_values_for :email, "a@b.cd", "e@f.gh"
25
- def should_allow_values_for(attribute, *values)
26
- values.each do |value|
27
- should("allow value of \"#{value}\" for #{attribute}") do
28
- get_error_from_writing_value(topic, attribute, value)
29
- end.nil
30
- end
31
- end
32
-
33
- # An ActiveRecord assertion that expects to fail with a given value or set of values for a given
34
- # attribute.
35
- #
36
- # Example
37
- # should_not_allow_values_for :email, "a"
38
- # should_not_allow_values_for :email, "a", "foo.bar"
39
- def should_not_allow_values_for(attribute, *values)
40
- values.each do |value|
41
- should("allow value of \"#{value}\" for #{attribute}") do
42
- get_error_from_writing_value(topic, attribute, value)
43
- end.exists
44
- end
45
- end
46
-
47
- # An ActiveRecord assertion that expects to fail with an attribute is not valid for record because the
48
- # value of the attribute is not unique. Requires the topic of the context to be a created record; one
49
- # that returns false for a call to +new_record?+.
50
- #
51
- # Example
52
- # should_validate_uniqueness_of :email
53
- def should_validate_uniqueness_of(attribute)
54
- asserts "topic is not a new record when testing uniqueness of #{attribute}" do
55
- !topic.new_record?
56
- end
57
-
58
- should "require #{attribute} to be a unique value" do
59
- copied_model = topic.class.new
60
- copied_value = topic.read_attribute(attribute)
61
- get_error_from_writing_value(copied_model, attribute, copied_value)
62
- end.exists
63
- end
64
- end # ContextMacros
65
-
66
- end # ActiveRecord
67
- end # Riot
68
-
69
- Riot::Context.instance_eval { include Riot::ActiveRecord::ContextMacros }
@@ -1,16 +0,0 @@
1
- module Riot
2
- module ActiveRecord
3
-
4
- module SituationMacros
5
- private
6
- def get_error_from_writing_value(model, attribute, value)
7
- model.write_attribute(attribute, value)
8
- model.valid?
9
- model.errors.on(attribute)
10
- end
11
- end # SituationMacros
12
-
13
- end # ActiveRecord
14
- end # Riot
15
-
16
- Riot::Situation.instance_eval { include Riot::ActiveRecord::SituationMacros }
@@ -1,18 +0,0 @@
1
- require 'teststrap'
2
-
3
- context "should_validate_presence_of" do
4
- setup_and_run_context("when attribute requires presence", 1, 0, 0) do |test_ctx|
5
- test_ctx.setup { Room.new }
6
- test_ctx.should_validate_presence_of :location
7
- end
8
-
9
- setup_and_run_context("when attribute does not require presence", 0, 1, 0) do |test_ctx|
10
- test_ctx.setup { Room.new }
11
- test_ctx.should_validate_presence_of :contents
12
- end
13
-
14
- setup_and_run_context("with multiple attributes required but not valid", 2, 0, 0) do |test_ctx|
15
- test_ctx.setup { Room.new }
16
- test_ctx.should_validate_presence_of :foo, :bar
17
- end
18
- end # should_validate_presence_of
@@ -1,20 +0,0 @@
1
- require 'teststrap'
2
-
3
- context "should_validate_uniqueness_of" do
4
-
5
- setup_and_run_context("without a persisted record", 0, 2, 0) do |test_ctx|
6
- test_ctx.setup { Room.new(:email => "foo@bar.baz") }
7
- test_ctx.should_validate_uniqueness_of :email
8
- end
9
-
10
- setup_and_run_context("with a persisted record", 2, 0, 0) do |test_ctx|
11
- test_ctx.setup { Room.create_with_good_data(:email => "foo@bar.baz") }
12
- test_ctx.should_validate_uniqueness_of :email
13
- end
14
-
15
- setup_and_run_context("with a persisted record but not validating uniqueness", 1, 1, 0) do |test_ctx|
16
- test_ctx.setup { Room.create_with_good_data(:email => "goo@car.caz") }
17
- test_ctx.should_validate_uniqueness_of :foo
18
- end
19
-
20
- end # should_validate_presence_of