rspec-radar 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/License +7 -0
- data/Rakefile +1 -0
- data/Readme.md +47 -0
- data/history.md +2 -0
- data/lib/rspec/radar.rb +38 -0
- data/rspec-radar.gemspec +18 -0
- metadata +80 -0
data/Gemfile
ADDED
data/License
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2013 Will Usher
|
2
|
+
|
3
|
+
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:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
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.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/Readme.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# rspec-radar
|
2
|
+
|
3
|
+
`rspec-radar` monkey patches `rspec-mocks` to prevent the stubbing of methods that don't exist. If you try and stub a method that doesn't exist, an `UndefinedMethodError` will be raised.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Step 1. Add the gem to your gemfile.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# Gemfile
|
11
|
+
gem 'rspec-radar'
|
12
|
+
```
|
13
|
+
|
14
|
+
Step 2. In the `spec_helper.rb` file, require 'rspec/radar'
|
15
|
+
``` ruby
|
16
|
+
# spec_helper.rb
|
17
|
+
require 'rspec/radar'
|
18
|
+
```
|
19
|
+
|
20
|
+
Now if you call `stub` or `should_receive` on a class or variable, it will check to see if the method exists and raise an exception if it doesn't.
|
21
|
+
|
22
|
+
## How it works
|
23
|
+
|
24
|
+
`rspec-radar` monkey patches `rspec-mocks` and will raise an excpetion if you stub a method that doesn't exist. Spefically it alters the method `visibility` in the class `MethodDouble`.
|
25
|
+
|
26
|
+
## Motivations
|
27
|
+
|
28
|
+
I want to be able to prevent the stubbing of nonexistent methods when using rspec. Currently there is no way to do this with [rspec-mocks](https://github.com/rspec/rspec-mocks). There is a popular mocking alternative called [rspec-fire](https://github.com/xaviershay/rspec-fire); however, rspec-fire isn't what I am after. rspec-fire introduces a different stubbing syntax and doesn't guard against a stub on an instance variable.
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
I'm more that happy to accept pull-requests. Also, I'm more than happy to receive crticism/suggestions for a better way to make this work.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
git clone https://github.com/wusher/rspec-radar.git
|
36
|
+
bundle install
|
37
|
+
bundle exec rake spec
|
38
|
+
```
|
39
|
+
|
40
|
+
## Future Plans
|
41
|
+
|
42
|
+
The following are features in want to add. _(not in any spefic order)_
|
43
|
+
|
44
|
+
* make it work when stubbing an entire class ( i.e. `stub(Avocado, fake_method: "boom") # raises undefined method error`)
|
45
|
+
* make it work with `any_instance` ( i.e. `Avocado.any_instance.stub(:fake_method) # raises undefined method error)`
|
46
|
+
* make the existance checks a configuration option
|
47
|
+
* setup `stub!` so it will skip the existance check`
|
data/history.md
ADDED
data/lib/rspec/radar.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rspec/mocks'
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
module RSpec
|
6
|
+
module Radar
|
7
|
+
class Error < StandardError; end
|
8
|
+
class UndefinedMethodError < Error; end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module RSpec
|
13
|
+
module Mocks
|
14
|
+
class MethodDouble < Hash
|
15
|
+
|
16
|
+
def current_object
|
17
|
+
instance_variable_get(("@object").intern)
|
18
|
+
end
|
19
|
+
|
20
|
+
def visibility
|
21
|
+
if TestDouble === self.current_object
|
22
|
+
'public'
|
23
|
+
elsif object_singleton_class.private_method_defined?(self.method_name)
|
24
|
+
'private'
|
25
|
+
elsif object_singleton_class.protected_method_defined?(self.method_name)
|
26
|
+
'protected'
|
27
|
+
elsif object_singleton_class.public_method_defined?(self.method_name)
|
28
|
+
'public'
|
29
|
+
elsif self.current_object.respond_to? self.method_name
|
30
|
+
'public'
|
31
|
+
else
|
32
|
+
raise Radar::UndefinedMethodError, "Attempt to stub unknown method '#{self.method_name}'"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
data/rspec-radar.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "rspec-radar"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.summary = "Monkey patch rspec-mocks to check for method existence"
|
5
|
+
s.license = "MIT"
|
6
|
+
s.authors = [ "wusher" ]
|
7
|
+
s.email = 'will@wusher.com'
|
8
|
+
s.homepage = 'http://github.com/wusher/rspec-radar'
|
9
|
+
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.has_rdoc = false
|
12
|
+
s.require_path = 'lib'
|
13
|
+
s.files = `git ls-files -- lib/*`.split("\n")
|
14
|
+
s.files += %w( Gemfile Readme.md history.md License Rakefile rspec-radar.gemspec )
|
15
|
+
|
16
|
+
s.add_dependency 'rspec', '~> 2.11'
|
17
|
+
s.add_development_dependency 'rake'
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-radar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- wusher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70269448354640 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.11'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70269448354640
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70269448354220 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70269448354220
|
36
|
+
description:
|
37
|
+
email: will@wusher.com
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files: []
|
41
|
+
files:
|
42
|
+
- lib/rspec/radar.rb
|
43
|
+
- Gemfile
|
44
|
+
- Readme.md
|
45
|
+
- history.md
|
46
|
+
- License
|
47
|
+
- Rakefile
|
48
|
+
- rspec-radar.gemspec
|
49
|
+
homepage: http://github.com/wusher/rspec-radar
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
hash: 2997100861076383367
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
hash: 2997100861076383367
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.8.17
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Monkey patch rspec-mocks to check for method existence
|
80
|
+
test_files: []
|