brightbox-rspec-rails-ext 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v1.0. Initial release
data/LICENCE ADDED
@@ -0,0 +1,11 @@
1
+ Released under the MIT Licence
2
+
3
+ Copyright (c) 2008 Brightbox Systems Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
11
+ See http://www.brightbox.co.uk/ for contact details.
data/Manifest ADDED
@@ -0,0 +1,8 @@
1
+ CHANGELOG
2
+ lib/controller_example_group.rb
3
+ lib/responses.rb
4
+ lib/rspec_rails_extensions.rb
5
+ LICENCE
6
+ Manifest
7
+ Rakefile
8
+ README.rdoc
data/README.rdoc ADDED
@@ -0,0 +1,64 @@
1
+ = RSpec-Rails Extensions
2
+
3
+ Some prettifications for RSpec Rails.
4
+
5
+ == Installation
6
+
7
+ sudo gem install brightbox-rspec-rails-ext --source=http://gems.github.com/
8
+
9
+ Then, within your config/environment.rb:
10
+
11
+ config.gem 'brightbox-rspec-rails-ext', :lib => 'rspec_rails_extensions', :source => 'http://gems.github.com'
12
+
13
+ == Matchers
14
+
15
+ Some helpful matchers for testing HTTP statuses beyond the default be_success (200 OK)
16
+
17
+ # looks for a 201 Created (useful for API CREATE calls)
18
+ response.should be_successfully_created
19
+
20
+ # expects a Location field with the given URL (useful for API CREATE calls)
21
+ response.should point_to(url)
22
+
23
+ # looks for a 422 response (invalid object)
24
+ response.should be_unprocessable
25
+
26
+ # looks for a 404
27
+ response.should be_not_found
28
+
29
+ # looks for a 401
30
+ response.should be_unauthorised
31
+
32
+ # looks for a 500
33
+ response.should be_an_error
34
+
35
+ == Specifying your controllers
36
+
37
+ One of the issues with writing standard controller specifications, using RSpec's inbuilt mock objects is that the "flow" of your specs feels wrong.
38
+
39
+ it "should display a page for a given thingy" do
40
+ @thingy = mock_model Thingy, :name => 'Wotsit'
41
+ Thingy.should_receive(:find).with('1').and_return(@thingy)
42
+
43
+ get :show, :id => '1'
44
+
45
+ response.should be_success
46
+ response.should render_template('thingies/show')
47
+ end
48
+
49
+ In other words you are setting expectations before you are doing the work - technically correct but it reads wrongly.
50
+
51
+ These extra helpers let you write specifications in a more english-like manner.
52
+
53
+ it "should display a page for a given thingy" do
54
+ @thingy = mock_model Thingy
55
+
56
+ on_getting :show, :id => '1' do
57
+ Thingy.should_receive(:find).with('1').and_return(@thingy)
58
+ end
59
+
60
+ response.should be_success
61
+ response.should render_template('thingies/show')
62
+ end
63
+
64
+ Isn't that lovely?
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'echoe'
2
+ Echoe.new('rspec-rails-ext') do | gem |
3
+ gem.author = 'Rahoul Baruah'
4
+ gem.summary = 'Helpers for prettying up your RSpec-Rails specifications'
5
+ gem.url = 'http://github.com/brightbox-rspec-rails-ext'
6
+ end
@@ -0,0 +1,65 @@
1
+ module Spec
2
+ module Rails
3
+ module Example
4
+ class ControllerExampleGroup < FunctionalExampleGroup
5
+ # Perform a pseudo HTTP GET, performing any code in the block beforehand. This is to allow you to set up expectations on your mocks whilst keeping the code readable.
6
+ # For example:
7
+ # on_getting :show, :id => '25' do
8
+ # @person = mock_model Person
9
+ # Person.should_receive(:find).with('25').and_return(@person)
10
+ # end
11
+ # If you want an XMLHTTPRequest pass in :as_xhr => true in the parameters
12
+ # (don't worry - it won't be passed to your controller)
13
+ # For example:
14
+ # on_getting :show, :id => '25', :as_xhr => true do
15
+ # whatever
16
+ # end
17
+ def on_getting page, parameters = {}
18
+ yield if block_given?
19
+ is_xhr_request = parameters.delete(:as_xhr)
20
+ if is_xhr_request
21
+ xhr :get, page, parameters
22
+ else
23
+ get page, parameters
24
+ end
25
+ end
26
+
27
+ # Perform a pseudo HTTP POST, performing any code in the block beforehand. This is to allow you to set up expectations on your mocks whilst keeping the code readable.
28
+ # As with on_getting, use :as_xhr => true to make this an XMLHTTPRequest
29
+ def on_posting_to page, parameters = {}
30
+ yield if block_given?
31
+ is_xhr_request = parameters.delete(:as_xhr)
32
+ if is_xhr_request
33
+ xhr :post, page, parameters
34
+ else
35
+ post page, parameters
36
+ end
37
+ end
38
+
39
+ # Perform a pseudo HTTP PUT, performing any code in the block beforehand. This is to allow you to set up expectations on your mocks whilst keeping the code readable.
40
+ # As with on_getting, use :as_xhr => true to make this an XMLHTTPRequest
41
+ def on_putting_to page, parameters = {}
42
+ yield if block_given?
43
+ is_xhr_request = parameters.delete(:as_xhr)
44
+ if is_xhr_request
45
+ xhr :put, page, parameters
46
+ else
47
+ put page, parameters
48
+ end
49
+ end
50
+
51
+ # Perform a pseudo HTTP DELETE, performing any code in the block beforehand. This is to allow you to set up expectations on your mocks whilst keeping the code readable.
52
+ # As with on_getting, use :as_xhr => true to make this an XMLHTTPRequest
53
+ def on_deleting_from page, parameters = {}
54
+ yield if block_given?
55
+ is_xhr_request = parameters.delete(:as_xhr)
56
+ if is_xhr_request
57
+ xhr :delete, page, parameters
58
+ else
59
+ delete page, parameters
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/responses.rb ADDED
@@ -0,0 +1,81 @@
1
+ =begin rdoc
2
+ Module with a set of matchers for examining ActionController::Response objects
3
+ =end
4
+ module ResponseMatchers
5
+ # test for a 201 response
6
+ def be_successfully_created
7
+ ResponseStatusMatcher.new "201 Created"
8
+ end
9
+
10
+ # test for a 422 response
11
+ def be_unprocessable
12
+ ResponseStatusMatcher.new "422 Unprocessable Entity"
13
+ end
14
+
15
+ # test for a 404 response
16
+ def be_not_found
17
+ ResponseStatusMatcher.new "404 Not Found"
18
+ end
19
+
20
+ # test for a 401 response
21
+ def be_unauthorised
22
+ ResponseStatusMatcher.new "401 Unauthorized"
23
+ end
24
+ alias :be_unauthorized :be_unauthorised
25
+
26
+ # test for a 500 internal server error
27
+ def be_an_error
28
+ ResponseStatusMatcher.new "500 Internal Error"
29
+ end
30
+
31
+ # test that the location points to a given URI
32
+ def point_to url
33
+ ResponseLocationMatcher.new url
34
+ end
35
+
36
+ # Response matcher that examines ActionController::Response headers for the required status code
37
+ class ResponseStatusMatcher
38
+ # Set up this matcher as required
39
+ def initialize status_code
40
+ @status_code = status_code
41
+ end
42
+
43
+ # Does the given target object match the required status code?
44
+ def matches? target
45
+ target.headers['Status'] == @status_code
46
+ end
47
+
48
+ # What do we tell the user when it fails?
49
+ def failure_message
50
+ "expected the response to be #{@status_code}"
51
+ end
52
+
53
+ # What do we tell the user when it shouldn't fail but does
54
+ def negative_failure_message
55
+ "expected the response to be different to #{@status_code}"
56
+ end
57
+ end
58
+
59
+ # Response matcher that examines ActionController::Response headers for the given location code
60
+ class ResponseLocationMatcher
61
+ def initialize url
62
+ @url = url
63
+ end
64
+
65
+ # Does the given target object match the required location?
66
+ def matches? target
67
+ target.headers['Location'] == @url
68
+ end
69
+
70
+ # What do we tell the user when it fails?
71
+ def failure_message
72
+ "expected the location header to be #{@url}"
73
+ end
74
+
75
+ # What do we tell the user when it shouldn't fail but does
76
+ def negative_failure_message
77
+ "expected the location header to be different to #{@url}"
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,2 @@
1
+ require 'matchers'
2
+ require 'controller_example_group'
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rspec-rails-ext}
5
+ s.version = "1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Rahoul Baruah"]
9
+ s.date = %q{2009-02-13}
10
+ s.description = %q{Helpers for prettying up your RSpec-Rails specifications}
11
+ s.email = %q{}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/controller_example_group.rb", "lib/responses.rb", "lib/rspec_rails_extensions.rb", "README.rdoc"]
13
+ s.files = ["CHANGELOG", "lib/controller_example_group.rb", "lib/responses.rb", "lib/rspec_rails_extensions.rb", "LICENCE", "Manifest", "Rakefile", "README.rdoc", "rspec-rails-ext.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/brightbox-rspec-rails-ext}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rspec-rails-ext", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{rspec-rails-ext}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Helpers for prettying up your RSpec-Rails specifications}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<echoe>, [">= 0"])
28
+ else
29
+ s.add_dependency(%q<echoe>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<echoe>, [">= 0"])
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brightbox-rspec-rails-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Rahoul Baruah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-13 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Helpers for prettying up your RSpec-Rails specifications
25
+ email: ""
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - CHANGELOG
32
+ - lib/controller_example_group.rb
33
+ - lib/responses.rb
34
+ - lib/rspec_rails_extensions.rb
35
+ - README.rdoc
36
+ files:
37
+ - CHANGELOG
38
+ - lib/controller_example_group.rb
39
+ - lib/responses.rb
40
+ - lib/rspec_rails_extensions.rb
41
+ - LICENCE
42
+ - Manifest
43
+ - Rakefile
44
+ - README.rdoc
45
+ - rspec-rails-ext.gemspec
46
+ has_rdoc: true
47
+ homepage: http://github.com/brightbox-rspec-rails-ext
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --line-numbers
51
+ - --inline-source
52
+ - --title
53
+ - Rspec-rails-ext
54
+ - --main
55
+ - README.rdoc
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "1.2"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: rspec-rails-ext
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: Helpers for prettying up your RSpec-Rails specifications
77
+ test_files: []
78
+