riot-rails 0.0.10.pre.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gitignore +4 -0
  2. data/CHANGELOG +144 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.markdown +31 -0
  5. data/Rakefile +43 -0
  6. data/VERSION +1 -0
  7. data/lib/riot/action_controller/context_macros/asserts_response.rb +15 -0
  8. data/lib/riot/action_controller/context_middleware.rb +45 -0
  9. data/lib/riot/action_controller/http_methods.rb +19 -0
  10. data/lib/riot/action_controller.rb +4 -0
  11. data/lib/riot/active_record/assertion_macros.rb +3 -0
  12. data/lib/riot/active_record/context_middleware.rb +15 -0
  13. data/lib/riot/active_record/database_macros.rb +58 -0
  14. data/lib/riot/active_record/reflection_macros.rb +106 -0
  15. data/lib/riot/active_record/transactional_middleware.rb +21 -0
  16. data/lib/riot/active_record/validation_macros.rb +187 -0
  17. data/lib/riot/active_record.rb +4 -0
  18. data/lib/riot/rails.rb +1 -0
  19. data/rails/init.rb +1 -0
  20. data/riot-rails.gemspec +130 -0
  21. data/test/action_controller/context_macros/asserts_response_test.rb +35 -0
  22. data/test/action_controller/context_middleware_test.rb +66 -0
  23. data/test/action_controller/delete_request_test.rb +45 -0
  24. data/test/action_controller/get_request_test.rb +37 -0
  25. data/test/action_controller/post_request_test.rb +45 -0
  26. data/test/action_controller/put_request_test.rb +45 -0
  27. data/test/action_controller/restful_delete_request_test.rb +28 -0
  28. data/test/action_controller/restful_get_request_test.rb +25 -0
  29. data/test/action_controller/restful_post_request_test.rb +25 -0
  30. data/test/action_controller/restful_put_request_test.rb +28 -0
  31. data/test/active_record/allowing_values_test.rb +64 -0
  32. data/test/active_record/attribute_is_invalid_test.rb +20 -0
  33. data/test/active_record/belongs_to_test.rb +22 -0
  34. data/test/active_record/context_middleware_test.rb +18 -0
  35. data/test/active_record/has_and_belongs_to_many_test.rb +22 -0
  36. data/test/active_record/has_database_index_on_test.rb +73 -0
  37. data/test/active_record/has_many_test.rb +22 -0
  38. data/test/active_record/has_one_test.rb +22 -0
  39. data/test/active_record/validates_length_of_test.rb +31 -0
  40. data/test/active_record/validates_presence_of_test.rb +14 -0
  41. data/test/active_record/validates_uniqueness_of_test.rb +23 -0
  42. data/test/rails_root/app/controllers/gremlins_controller.rb +21 -0
  43. data/test/rails_root/app/controllers/parties_controller.rb +17 -0
  44. data/test/rails_root/app/controllers/rooms_controller.rb +22 -0
  45. data/test/rails_root/app/views/rendered_templates/foo_bar.html.erb +0 -0
  46. data/test/rails_root/config/database.yml +4 -0
  47. data/test/rails_root/config/environment.rb +46 -0
  48. data/test/rails_root/config/routes.rb +7 -0
  49. data/test/rails_root/db/schema.rb +8 -0
  50. data/test/teststrap.rb +84 -0
  51. data/test/transactional_middleware_test.rb +26 -0
  52. metadata +201 -0
@@ -0,0 +1,187 @@
1
+ module RiotRails
2
+ module ActiveRecord
3
+ protected
4
+
5
+ class ValidationAssertionMacro < Riot::AssertionMacro
6
+ private
7
+ def errors_from_writing_value(model, attribute, value)
8
+ # TODO: Fix me to not use __send__
9
+ model.__send__(:write_attribute, attribute, value)
10
+ model.valid?
11
+ model.errors[attribute]
12
+ end
13
+
14
+ def errors_from_writing_value?(*args)
15
+ errors_from_writing_value(*args).any?
16
+ end
17
+ end
18
+
19
+ public
20
+
21
+ # An ActiveRecord assertion that expects to pass with a given value or set of values for a given
22
+ # attribute.
23
+ #
24
+ # rails_context User do
25
+ # asserts_topic.allows_values_for :email, "a@b.cd"
26
+ # asserts_topic.allows_values_for :email, "a@b.cd", "e@f.gh"
27
+ # end
28
+ class AllowsValuesForMacro < ValidationAssertionMacro
29
+ register :allows_values_for
30
+
31
+ def evaluate(actual, attribute, *values)
32
+ bad_values = []
33
+ values.each do |value|
34
+ bad_values << value if errors_from_writing_value?(actual, attribute, value)
35
+ end
36
+ bad_values.empty? ? pass : fail(expected_message(attribute).to_allow_values(bad_values))
37
+ end
38
+ end
39
+
40
+ # An ActiveRecord assertion that expects to fail with a given value or set of values for a given
41
+ # attribute.
42
+ #
43
+ # rails_context User do
44
+ # asserts_topic.does_not_allow_values_for :email, "a"
45
+ # asserts_topic.does_not_allow_values_for :email, "a@b", "e f@g.h"
46
+ # end
47
+ class DoesNotAllowValuesForMacro < ValidationAssertionMacro
48
+ register :does_not_allow_values_for
49
+ def evaluate(actual, attribute, *values)
50
+ good_values = []
51
+ values.each do |value|
52
+ good_values << value unless errors_from_writing_value?(actual, attribute, value)
53
+ end
54
+ good_values.empty? ? pass : fail(expected_message(attribute).not_to_allow_values(good_values))
55
+ end
56
+ end
57
+
58
+ # An ActiveRecord assertion that expects to fail with invalid value for an attribute. Optionally, the
59
+ # error message can be provided as the exact string or as regular expression.
60
+ #
61
+ # rails_context User do
62
+ # asserts_topic.is_invalid_when :email, "fake", "is invalid"
63
+ # asserts_topic.is_invalid_when :email, "another fake", /invalid/
64
+ # end
65
+ class IsInvalidWhenMacro < ValidationAssertionMacro
66
+ register :is_invalid_when
67
+
68
+ def evaluate(actual, attribute, value, expected_error=nil)
69
+ actual_errors = errors_from_writing_value(actual, attribute, value)
70
+ if actual_errors.empty?
71
+ fail expected_message(attribute).to_be_invalid_when_value_is(value)
72
+ elsif expected_error && !has_error_message?(expected_error, actual_errors)
73
+ fail expected_message(attribute).to_be_invalid_with_error_message(expected_error)
74
+ else
75
+ pass new_message.attribute(attribute).is_invalid
76
+ end
77
+ end
78
+ private
79
+ def has_error_message?(expected, errors)
80
+ return true unless expected
81
+ expected.kind_of?(Regexp) ? errors.any? {|e| e =~ expected } : errors.any? {|e| e == expected }
82
+ end
83
+ end
84
+
85
+ # An ActiveRecord assertion that expects to fail when a given attribute is validated after a nil value
86
+ # is provided to it.
87
+ #
88
+ # rails_context User do
89
+ # asserts_topic.validates_presence_of(:name)
90
+ # end
91
+ class ValidatesPresenceOfMacro < ValidationAssertionMacro
92
+ register :validates_presence_of
93
+
94
+ def evaluate(actual, attribute)
95
+ if errors_from_writing_value?(actual, attribute, nil)
96
+ pass new_message.validates_presence_of(attribute)
97
+ else
98
+ fail expected_message.to_validate_presence_of(attribute)
99
+ end
100
+ end
101
+ end
102
+
103
+ # An ActiveRecord assertion that expects to fail with an attribute is not valid for record because the
104
+ # value of the attribute is not unique. Requires the topic of the context to be a created record; one
105
+ # that returns false for a call to +new_record?+.
106
+ #
107
+ # rails_context User do
108
+ # setup { User.create(:email => "a@b.cde", ... ) }
109
+ # asserts_topic.validates_uniqueness_of :email
110
+ # end
111
+ class ValidatesUniquenessOfMacro < ValidationAssertionMacro
112
+ register :validates_uniqueness_of
113
+
114
+ def evaluate(actual, attribute)
115
+ actual_record = actual
116
+ if actual_record.new_record?
117
+ fail new_message.must_use_a_persisted_record_when_testing_uniqueness_of(attribute)
118
+ else
119
+ copied_model = actual_record.class.new
120
+ actual_record.attributes.each do |dup_attribute, dup_value|
121
+ copied_model.__send__(:write_attribute, dup_attribute, dup_value)
122
+ end
123
+ copied_value = actual_record.__send__(:read_attribute, attribute)
124
+ if errors_from_writing_value?(copied_model, attribute, copied_value)
125
+ pass new_message(attribute).is_unique
126
+ else
127
+ fail expected_message.to_fail_because(attribute).is_not_unique
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ # An ActiveRecord assertion to test that the length of the value of an attribute must be within a
134
+ # specified range. Assertion fails if: there are errors when min/max characters are used, there are no
135
+ # errors when min-1/max+1 characters are used.
136
+ #
137
+ # rails_context User do
138
+ # asserts_topic.validates_length_of :name, (2..36)
139
+ # end
140
+ #
141
+ # TODO: allow for options on what to validate
142
+ class ValidatesLengthOfMacro < ValidationAssertionMacro
143
+ register :validates_length_of
144
+
145
+ def evaluate(actual, attribute, range)
146
+ min, max = range.first, range.last
147
+ [min, max].each do |length|
148
+ if errors_from_writing_value?(actual, attribute, "r" * length)
149
+ return fail(new_message(attribute).should_be_able_to_be(length).characters)
150
+ end
151
+ end
152
+
153
+ if (min-1) > 0 && !errors_from_writing_value?(actual, attribute, "r" * (min-1))
154
+ fail new_message(attribute).expected_to_be_more_than(min - 1).characters
155
+ elsif !errors_from_writing_value?(actual, attribute, "r" * (max+1))
156
+ fail new_message(attribute).expected_to_be_less_than(max + 1).characters
157
+ else
158
+ pass new_message.validates_length_of(attribute).is_within(range)
159
+ end
160
+ end
161
+ end
162
+
163
+ # An ActiveRecord assertion to test that an attribute is invalid along with the expected error message
164
+ # (or error message partial). The assumption is that a value has already been set.
165
+ #
166
+ # rails_context User do
167
+ # hookup { topic.bio = "I'm a goofy clown" }
168
+ # asserts_topic.attribute_is_invalid :bio, "cannot contain adjectives"
169
+ # end
170
+ class AttributeIsInvalidMacro < ValidationAssertionMacro
171
+ register :attribute_is_invalid
172
+
173
+ def evaluate(actual, attribute, error_message)
174
+ actual.valid?
175
+ errors = actual.errors[attribute]
176
+ if errors.empty?
177
+ fail new_message(attribute).expected_to_be_invalid
178
+ elsif errors.include?(error_message)
179
+ pass new_message(attribute).is_invalid
180
+ else
181
+ fail new_message(attribute).is_invalid.but(error_message).is_not_a_valid_error_message
182
+ end
183
+ end
184
+ end
185
+
186
+ end # ActiveRecord
187
+ end # RiotRails
@@ -0,0 +1,4 @@
1
+ require 'riot/rails'
2
+ require 'riot/active_record/assertion_macros'
3
+ require 'riot/active_record/context_middleware'
4
+ require 'riot/active_record/transactional_middleware'
data/lib/riot/rails.rb ADDED
@@ -0,0 +1 @@
1
+ require 'riot'
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'riot/rails' if RAILS_ENV == 'test'
@@ -0,0 +1,130 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{riot-rails}
8
+ s.version = "0.0.10.pre.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Justin 'Gus' Knowlden"]
12
+ s.date = %q{2010-06-04}
13
+ s.description = %q{Riot specific test support for Rails apps. Protest the slow app.}
14
+ s.email = %q{gus@gusg.us}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "CHANGELOG",
21
+ "MIT-LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/riot/action_controller.rb",
26
+ "lib/riot/action_controller/context_macros/asserts_response.rb",
27
+ "lib/riot/action_controller/context_middleware.rb",
28
+ "lib/riot/action_controller/http_methods.rb",
29
+ "lib/riot/active_record.rb",
30
+ "lib/riot/active_record/assertion_macros.rb",
31
+ "lib/riot/active_record/context_middleware.rb",
32
+ "lib/riot/active_record/database_macros.rb",
33
+ "lib/riot/active_record/reflection_macros.rb",
34
+ "lib/riot/active_record/transactional_middleware.rb",
35
+ "lib/riot/active_record/validation_macros.rb",
36
+ "lib/riot/rails.rb",
37
+ "rails/init.rb",
38
+ "riot-rails.gemspec",
39
+ "test/action_controller/context_macros/asserts_response_test.rb",
40
+ "test/action_controller/context_middleware_test.rb",
41
+ "test/action_controller/delete_request_test.rb",
42
+ "test/action_controller/get_request_test.rb",
43
+ "test/action_controller/post_request_test.rb",
44
+ "test/action_controller/put_request_test.rb",
45
+ "test/action_controller/restful_delete_request_test.rb",
46
+ "test/action_controller/restful_get_request_test.rb",
47
+ "test/action_controller/restful_post_request_test.rb",
48
+ "test/action_controller/restful_put_request_test.rb",
49
+ "test/active_record/allowing_values_test.rb",
50
+ "test/active_record/attribute_is_invalid_test.rb",
51
+ "test/active_record/belongs_to_test.rb",
52
+ "test/active_record/context_middleware_test.rb",
53
+ "test/active_record/has_and_belongs_to_many_test.rb",
54
+ "test/active_record/has_database_index_on_test.rb",
55
+ "test/active_record/has_many_test.rb",
56
+ "test/active_record/has_one_test.rb",
57
+ "test/active_record/validates_length_of_test.rb",
58
+ "test/active_record/validates_presence_of_test.rb",
59
+ "test/active_record/validates_uniqueness_of_test.rb",
60
+ "test/rails_root/app/controllers/gremlins_controller.rb",
61
+ "test/rails_root/app/controllers/parties_controller.rb",
62
+ "test/rails_root/app/controllers/rooms_controller.rb",
63
+ "test/rails_root/app/views/rendered_templates/foo_bar.html.erb",
64
+ "test/rails_root/config/database.yml",
65
+ "test/rails_root/config/environment.rb",
66
+ "test/rails_root/config/routes.rb",
67
+ "test/rails_root/db/schema.rb",
68
+ "test/teststrap.rb",
69
+ "test/transactional_middleware_test.rb"
70
+ ]
71
+ s.homepage = %q{http://github.com/thumblemonks/riot-rails}
72
+ s.rdoc_options = ["--charset=UTF-8"]
73
+ s.require_paths = ["lib"]
74
+ s.rubygems_version = %q{1.3.6}
75
+ s.summary = %q{Riot specific test support for Rails apps}
76
+ s.test_files = [
77
+ "test/action_controller/context_macros/asserts_response_test.rb",
78
+ "test/action_controller/context_middleware_test.rb",
79
+ "test/action_controller/delete_request_test.rb",
80
+ "test/action_controller/get_request_test.rb",
81
+ "test/action_controller/post_request_test.rb",
82
+ "test/action_controller/put_request_test.rb",
83
+ "test/action_controller/restful_delete_request_test.rb",
84
+ "test/action_controller/restful_get_request_test.rb",
85
+ "test/action_controller/restful_post_request_test.rb",
86
+ "test/action_controller/restful_put_request_test.rb",
87
+ "test/active_record/allowing_values_test.rb",
88
+ "test/active_record/attribute_is_invalid_test.rb",
89
+ "test/active_record/belongs_to_test.rb",
90
+ "test/active_record/context_middleware_test.rb",
91
+ "test/active_record/has_and_belongs_to_many_test.rb",
92
+ "test/active_record/has_database_index_on_test.rb",
93
+ "test/active_record/has_many_test.rb",
94
+ "test/active_record/has_one_test.rb",
95
+ "test/active_record/validates_length_of_test.rb",
96
+ "test/active_record/validates_presence_of_test.rb",
97
+ "test/active_record/validates_uniqueness_of_test.rb",
98
+ "test/rails_root/app/controllers/gremlins_controller.rb",
99
+ "test/rails_root/app/controllers/parties_controller.rb",
100
+ "test/rails_root/app/controllers/rooms_controller.rb",
101
+ "test/rails_root/config/environment.rb",
102
+ "test/rails_root/config/routes.rb",
103
+ "test/rails_root/db/schema.rb",
104
+ "test/teststrap.rb",
105
+ "test/transactional_middleware_test.rb"
106
+ ]
107
+
108
+ if s.respond_to? :specification_version then
109
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
110
+ s.specification_version = 3
111
+
112
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
113
+ s.add_runtime_dependency(%q<riot>, [">= 0.11.0"])
114
+ s.add_runtime_dependency(%q<rack-test>, [">= 0.5.3"])
115
+ s.add_development_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
116
+ s.add_development_dependency(%q<actionpack>, [">= 3.0.0.beta3"])
117
+ else
118
+ s.add_dependency(%q<riot>, [">= 0.11.0"])
119
+ s.add_dependency(%q<rack-test>, [">= 0.5.3"])
120
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
121
+ s.add_dependency(%q<actionpack>, [">= 3.0.0.beta3"])
122
+ end
123
+ else
124
+ s.add_dependency(%q<riot>, [">= 0.11.0"])
125
+ s.add_dependency(%q<rack-test>, [">= 0.5.3"])
126
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
127
+ s.add_dependency(%q<actionpack>, [">= 3.0.0.beta3"])
128
+ end
129
+ end
130
+
@@ -0,0 +1,35 @@
1
+ require 'teststrap'
2
+
3
+ context "An asserts_response context macro" do
4
+ context "for context not representing an ActionController" do
5
+ setup { Riot::Context.new("Hello") {} }
6
+ asserts("asserts_response returns") { topic.asserts_response }.raises(NoMethodError)
7
+ end # for context not representing an ActionController
8
+
9
+ helper(:situation) do
10
+ a_situation = Riot::Situation.new
11
+ a_situation.helper(:response) { OpenStruct.new(:status => 200) }
12
+ a_situation
13
+ end
14
+
15
+ setup do
16
+ Riot::Context.new(RoomsController) {}
17
+ end
18
+
19
+ context "when referenced without arguments" do
20
+ setup { topic.asserts_response }
21
+
22
+ asserts_topic.kind_of(Riot::Assertion)
23
+ asserts(:to_s).equals("asserts response")
24
+
25
+ asserts("the result of assertion responds to :status") do
26
+ topic.responds_to(:status).run(situation).first
27
+ end.equals(:pass)
28
+ end # when referenced without arguments
29
+
30
+ context "with method name provided" do
31
+ setup { topic.asserts_response(:status).equals(200) }
32
+ asserts(:to_s).equals("asserts response #status")
33
+ asserts("output of running the test") { topic.run(situation).first }.equals(:pass)
34
+ end
35
+ end # An asserts_response context macro
@@ -0,0 +1,66 @@
1
+ require 'teststrap'
2
+
3
+ context "ActionController middleware" do
4
+ setup_test_context
5
+
6
+ helper(:assigned) { |var| topic.instance_variable_get(var) }
7
+
8
+ context "pre-request state" do
9
+ setup do
10
+ situation = Riot::Situation.new
11
+ topic.context(RoomsController) {}.local_run(Riot::SilentReporter.new, situation)
12
+ situation
13
+ end
14
+
15
+ asserts(:topic).equals(RoomsController)
16
+
17
+ asserts("@app") { assigned(:@app).to_s }.equals { ::Rails.application.to_s }
18
+ asserts("helper app") { topic.app.to_s }.equals { assigned(:@app).to_s }
19
+
20
+ asserts("@env") { assigned(:@env) }.equals({})
21
+ asserts("helper env") { topic.env }.equals { assigned(:@env) }
22
+
23
+ asserts("helper controller") { topic.controller }.equals { assigned(:@controller) }
24
+
25
+ asserts("request before any request made") do
26
+ topic.request
27
+ end.raises(Exception, "No request made yet")
28
+
29
+ asserts("response before any request made") do
30
+ topic.response
31
+ end.raises(Exception, "No response since no request made yet")
32
+
33
+ asserts_topic.responds_to(:get)
34
+ asserts_topic.responds_to(:post)
35
+ asserts_topic.responds_to(:put)
36
+ asserts_topic.responds_to(:delete)
37
+ end # pre-request state
38
+
39
+ context "post-request state" do
40
+ setup do
41
+ situation = Riot::Situation.new
42
+ topic.context(RoomsController) do
43
+ setup { get("/rooms") }
44
+ end.local_run(Riot::SilentReporter.new, situation)
45
+ situation
46
+ end
47
+
48
+ asserts(:topic).kind_of(Array)
49
+ asserts(:topic).size(3)
50
+ asserts("#topic first element response status") { topic.topic[0] }.equals(200)
51
+ asserts("#topic second element") { topic.topic[1] }.kind_of(Hash)
52
+ asserts("#topic second element") { topic.topic[1] }.includes("ETag")
53
+ asserts("#topic last element") { topic.topic[2] }.kind_of(ActionDispatch::Response)
54
+
55
+ asserts("helper env") { topic.env }.includes("action_controller.instance")
56
+
57
+ asserts("helper controller") { topic.controller }.kind_of(RoomsController)
58
+ asserts("env['action_controller.instance']") do
59
+ topic.env["action_controller.instance"]
60
+ end.kind_of(RoomsController)
61
+
62
+ asserts("helper request") { topic.request }.kind_of(Rack::Request)
63
+ asserts("helper response") { topic.response }.kind_of(Rack::Response)
64
+ end # post-request state
65
+
66
+ end # ActionController middleware
@@ -0,0 +1,45 @@
1
+ require 'teststrap'
2
+
3
+ context RoomsController do
4
+
5
+ asserts("DELETEs unknown action for existing controller") do
6
+ delete "/rooms/blah"
7
+ end.raises(AbstractController::ActionNotFound, "The action 'blah' could not be found")
8
+
9
+ context "for a DELETE request" do
10
+ helper(:action_name) { env['action_dispatch.request.path_parameters'][:action] }
11
+
12
+ context "without parameters" do
13
+ setup { delete "/rooms/destroy" }
14
+
15
+ asserts("request method") { request.request_method }.equals("DELETE")
16
+ asserts("action name") { action_name }.equals("destroy")
17
+ asserts("response status") { response.status }.equals(200)
18
+ asserts("response body") { response.body }.equals { "destroyed #{controller.params.inspect}" }
19
+
20
+ context "response headers" do
21
+ setup { response.headers.keys }
22
+ asserts_topic.includes("Content-Type")
23
+ asserts_topic.includes("ETag")
24
+ asserts_topic.includes("Cache-Control")
25
+ end # response headers
26
+ end # without parameters
27
+
28
+ context "with parameters" do
29
+ setup { delete "/rooms/destroy", {:momma => "loves you", "love_you_too" => "mom"} }
30
+
31
+ asserts("request method") { request.request_method }.equals("DELETE")
32
+ asserts("action name") { action_name }.equals("destroy")
33
+ asserts("response status") { response.status }.equals(200)
34
+
35
+ asserts("response body") { response.body }.equals do
36
+ "destroyed #{controller.params.inspect}"
37
+ end
38
+
39
+ asserts("params") { controller.params }.includes("momma")
40
+ asserts("params") { controller.params }.includes("love_you_too")
41
+ end # with parameters
42
+
43
+ end # for a DELETE request
44
+
45
+ end # RoomsController
@@ -0,0 +1,37 @@
1
+ require 'teststrap'
2
+
3
+ context RoomsController do
4
+
5
+ asserts("GETs unknown action for existing controller") do
6
+ get "/rooms/blah"
7
+ end.raises(AbstractController::ActionNotFound, "The action 'blah' could not be found")
8
+
9
+ context "for a GET request" do
10
+ helper(:action_name) { env['action_dispatch.request.path_parameters'][:action] }
11
+
12
+ context "without parameters" do
13
+ setup { get "/rooms/index" }
14
+
15
+ asserts("action name") { action_name }.equals("index")
16
+ asserts("response status") { response.status }.equals(200)
17
+ asserts("response body") { response.body }.equals("foo")
18
+ asserts("request method") { request.request_method }.equals("GET")
19
+
20
+ context "response headers" do
21
+ setup { response.headers.keys }
22
+ asserts_topic.includes("Content-Type")
23
+ asserts_topic.includes("ETag")
24
+ asserts_topic.includes("Cache-Control")
25
+ end # response headers
26
+ end # without parameters
27
+
28
+ context "with parameters" do
29
+ setup { get "/rooms/echo_params", {:name => "Juiceton", :foo => "blaz"} }
30
+
31
+ asserts("action name") { action_name }.equals("echo_params")
32
+ asserts("response body") { response.body }.equals("controller=rooms,foo=blaz,name=Juiceton")
33
+ end # with with parameters
34
+
35
+ end # for a GET request
36
+
37
+ end # RoomsController
@@ -0,0 +1,45 @@
1
+ require 'teststrap'
2
+
3
+ context RoomsController do
4
+
5
+ asserts("POSTs unknown action for existing controller") do
6
+ post "/rooms/blah"
7
+ end.raises(AbstractController::ActionNotFound, "The action 'blah' could not be found")
8
+
9
+ context "for a POST request" do
10
+ helper(:action_name) { env['action_dispatch.request.path_parameters'][:action] }
11
+
12
+ context "without parameters" do
13
+ setup { post "/rooms/create" }
14
+
15
+ asserts("request method") { request.request_method }.equals("POST")
16
+ asserts("action name") { action_name }.equals("create")
17
+ asserts("response status") { response.status }.equals(200)
18
+ asserts("response body") { response.body }.equals { "created #{controller.params.inspect}" }
19
+
20
+ context "response headers" do
21
+ setup { response.headers.keys }
22
+ asserts_topic.includes("Content-Type")
23
+ asserts_topic.includes("ETag")
24
+ asserts_topic.includes("Cache-Control")
25
+ end # response headers
26
+ end # without parameters
27
+
28
+ context "with parameters" do
29
+ setup { post "/rooms/create", {:momma => "loves you", "love_you_too" => "mom"} }
30
+
31
+ asserts("request method") { request.request_method }.equals("POST")
32
+ asserts("action name") { action_name }.equals("create")
33
+ asserts("response status") { response.status }.equals(200)
34
+
35
+ asserts("response body") { response.body }.equals do
36
+ "created #{controller.params.inspect}"
37
+ end
38
+
39
+ asserts("params") { controller.params }.includes("momma")
40
+ asserts("params") { controller.params }.includes("love_you_too")
41
+ end # with parameters
42
+
43
+ end # for a POST request
44
+
45
+ end # RoomsController
@@ -0,0 +1,45 @@
1
+ require 'teststrap'
2
+
3
+ context RoomsController do
4
+
5
+ asserts("PUTs unknown action for existing controller") do
6
+ put "/rooms/blah"
7
+ end.raises(AbstractController::ActionNotFound, "The action 'blah' could not be found")
8
+
9
+ context "for a PUT request" do
10
+ helper(:action_name) { env['action_dispatch.request.path_parameters'][:action] }
11
+
12
+ context "without parameters" do
13
+ setup { put "/rooms/update" }
14
+
15
+ asserts("request method") { request.request_method }.equals("PUT")
16
+ asserts("action name") { action_name }.equals("update")
17
+ asserts("response status") { response.status }.equals(200)
18
+ asserts("response body") { response.body }.equals { "updated #{controller.params.inspect}" }
19
+
20
+ context "response headers" do
21
+ setup { response.headers.keys }
22
+ asserts_topic.includes("Content-Type")
23
+ asserts_topic.includes("ETag")
24
+ asserts_topic.includes("Cache-Control")
25
+ end # response headers
26
+ end # without parameters
27
+
28
+ context "with parameters" do
29
+ setup { put "/rooms/update", {:momma => "loves you", "love_you_too" => "mom"} }
30
+
31
+ asserts("request method") { request.request_method }.equals("PUT")
32
+ asserts("action name") { action_name }.equals("update")
33
+ asserts("response status") { response.status }.equals(200)
34
+
35
+ asserts("response body") { response.body }.equals do
36
+ "updated #{controller.params.inspect}"
37
+ end
38
+
39
+ asserts("params") { controller.params }.includes("momma")
40
+ asserts("params") { controller.params }.includes("love_you_too")
41
+ end # with parameters
42
+
43
+ end # for a PUT request
44
+
45
+ end # RoomsController
@@ -0,0 +1,28 @@
1
+ require 'teststrap'
2
+
3
+ context "RESTful DELETE request on resource" do
4
+
5
+ context GremlinsController do
6
+ setup { delete("/gremlins/2") }
7
+
8
+ asserts("request method") { request.request_method }.equals("DELETE")
9
+ asserts("controller name") { controller.controller_name }.equals("gremlins")
10
+ asserts("action name") { controller.action_name }.equals("destroy")
11
+ asserts("id param") { controller.params["id"] }.equals("2")
12
+ asserts("response body") { response.body }.equals("spendin' money")
13
+ end # on a top level resource
14
+
15
+ context PartiesController do
16
+ setup { delete("/gremlins/2/parties/3", "foo" => "bar") }
17
+
18
+ asserts("request method") { request.request_method }.equals("DELETE")
19
+ asserts("controller name") { controller.controller_name }.equals("parties")
20
+ asserts("action name") { controller.action_name }.equals("destroy")
21
+
22
+ asserts("gremlin_id param") { controller.params["gremlin_id"] }.equals("2")
23
+ asserts("id param") { controller.params["id"] }.equals("3")
24
+ asserts("foo param") { controller.params["foo"] }.equals("bar")
25
+
26
+ asserts("response body") { response.body }.equals("all gone")
27
+ end # on a nested resource
28
+ end # RESTful DELETE request
@@ -0,0 +1,25 @@
1
+ require 'teststrap'
2
+
3
+ context "RESTful GET request on resource" do
4
+
5
+ context GremlinsController do
6
+ setup { get("/gremlins/1") }
7
+
8
+ asserts("request method") { request.request_method }.equals("GET")
9
+ asserts("controller name") { controller.controller_name }.equals("gremlins")
10
+ asserts("action name") { controller.action_name }.equals("show")
11
+ asserts("id param") { controller.params["id"] }.equals("1")
12
+ asserts("response body") { response.body }.equals("show me the money")
13
+ end # on a top level resource
14
+
15
+ context PartiesController do
16
+ setup { get("/gremlins/1/parties/2") }
17
+
18
+ asserts("request method") { request.request_method }.equals("GET")
19
+ asserts("controller name") { controller.controller_name }.equals("parties")
20
+ asserts("action name") { controller.action_name }.equals("show")
21
+ asserts("gremlin_id param") { controller.params["gremlin_id"] }.equals("1")
22
+ asserts("id param") { controller.params["id"] }.equals("2")
23
+ asserts("response body") { response.body }.equals("woot")
24
+ end # on a nested resource
25
+ end # RESTful GET request