rspec-rails-mocha 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +21 -2
- data/lib/rspec/rails/mocha.rb +35 -18
- data/spec/mocha_spec.rb +1 -11
- metadata +7 -13
- data/lib/rspec-rails-mocha.rb +0 -7
data/README.markdown
CHANGED
@@ -3,13 +3,28 @@ rspec-rails Mocha extension
|
|
3
3
|
|
4
4
|
Functionality of `mock_model` and `stub_model` from rspec-rails for those using Mocha.
|
5
5
|
|
6
|
-
Installation:
|
6
|
+
Installation for **Rails 3** (rspec-rails v2.6 and above):
|
7
7
|
|
8
8
|
# Gemfile
|
9
|
+
group :development, :test do
|
10
|
+
gem 'rspec-rails', '~> 2.4'
|
11
|
+
end
|
12
|
+
|
9
13
|
group :test do
|
10
|
-
gem 'rspec-rails-mocha'
|
14
|
+
gem 'rspec-rails-mocha', '~> 0.3.1', :require => false
|
11
15
|
end
|
12
16
|
|
17
|
+
# spec/spec_helper.rb
|
18
|
+
require 'rspec/rails'
|
19
|
+
require 'rspec/rails/mocha'
|
20
|
+
|
21
|
+
For **Rails 2** (rspec-rails v1.3.x), use the '0.2.x' branch:
|
22
|
+
|
23
|
+
gem 'rspec-rails-mocha', '~> 0.2.1'
|
24
|
+
|
25
|
+
Then, for any version of Rails, ensure that your "spec_helper.rb" file
|
26
|
+
**doesn't contain** `config.mock_with :rspec`.
|
27
|
+
|
13
28
|
Usage:
|
14
29
|
|
15
30
|
describe "Mocha plugin" do
|
@@ -20,3 +35,7 @@ Usage:
|
|
20
35
|
person.id.should == 66
|
21
36
|
end
|
22
37
|
end
|
38
|
+
|
39
|
+
You should be able to use `mock_model` and `stub_model` in the same way
|
40
|
+
you use them when mocking with rspec instead of mocha. [Here are the
|
41
|
+
docs](http://relishapp.com/rspec/rspec-rails/v/2-6/dir/mocks/mock-model).
|
data/lib/rspec/rails/mocha.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
|
+
require 'active_support/core_ext'
|
1
2
|
require 'active_model'
|
2
3
|
|
3
4
|
module RSpec
|
4
5
|
module Rails
|
6
|
+
|
7
|
+
unless defined? IllegalDataAccessException
|
8
|
+
class IllegalDataAccessException < StandardError; end
|
9
|
+
end
|
10
|
+
|
5
11
|
module Mocha
|
12
|
+
|
6
13
|
module ActiveModelInstanceMethods
|
7
14
|
def as_new_record
|
8
|
-
self.stubs(:persisted?
|
9
|
-
self.stubs(:id) { nil }
|
15
|
+
self.stubs(:persisted? => false, :id => nil)
|
10
16
|
self
|
11
17
|
end
|
12
18
|
|
@@ -21,8 +27,11 @@ module RSpec
|
|
21
27
|
|
22
28
|
module ActiveRecordInstanceMethods
|
23
29
|
def destroy
|
24
|
-
self.stubs(:persisted?
|
25
|
-
|
30
|
+
self.stubs(:persisted? => false, :id => nil)
|
31
|
+
end
|
32
|
+
|
33
|
+
def [](key)
|
34
|
+
send(key)
|
26
35
|
end
|
27
36
|
|
28
37
|
def new_record?
|
@@ -71,21 +80,23 @@ It received #{model_class.inspect}
|
|
71
80
|
EOM
|
72
81
|
end
|
73
82
|
|
74
|
-
stubs = stubs.reverse_merge
|
83
|
+
stubs = stubs.reverse_merge \
|
84
|
+
:id => next_id,
|
85
|
+
:destroyed? => false,
|
86
|
+
:marked_for_destruction? => false,
|
87
|
+
:blank? => false
|
88
|
+
|
75
89
|
stubs = stubs.reverse_merge(:persisted? => !!stubs[:id])
|
76
|
-
stubs = stubs.reverse_merge(:destroyed? => false)
|
77
|
-
stubs = stubs.reverse_merge(:marked_for_destruction? => false)
|
78
|
-
stubs = stubs.reverse_merge(:errors => stub("errors", :count => 0, :[] => [], :empty? => true))
|
79
90
|
|
80
91
|
stub("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
|
81
92
|
m.extend ActiveModelInstanceMethods
|
82
|
-
|
83
|
-
|
93
|
+
m.singleton_class.__send__ :include, ActiveModel::Conversion
|
94
|
+
m.singleton_class.__send__ :include, ActiveModel::Validations
|
84
95
|
if defined?(ActiveRecord)
|
85
96
|
m.extend ActiveRecordInstanceMethods
|
86
97
|
[:save, :update_attributes].each do |key|
|
87
98
|
if stubs[key] == false
|
88
|
-
m.errors.stubs(:empty?
|
99
|
+
m.errors.stubs(:empty? => false)
|
89
100
|
end
|
90
101
|
end
|
91
102
|
end
|
@@ -108,10 +119,6 @@ EOM
|
|
108
119
|
def to_s
|
109
120
|
"#{model_class.name}_#{to_param}"
|
110
121
|
end
|
111
|
-
|
112
|
-
def to_key
|
113
|
-
[id]
|
114
|
-
end
|
115
122
|
CODE
|
116
123
|
yield m if block_given?
|
117
124
|
end
|
@@ -119,10 +126,13 @@ EOM
|
|
119
126
|
|
120
127
|
module ActiveModelStubExtensions
|
121
128
|
def as_new_record
|
122
|
-
self.stubs(:persisted?
|
123
|
-
self.stubs(:id) { nil }
|
129
|
+
self.stubs(:persisted? => false, :id => nil)
|
124
130
|
self
|
125
131
|
end
|
132
|
+
|
133
|
+
def persisted?
|
134
|
+
true
|
135
|
+
end
|
126
136
|
end
|
127
137
|
|
128
138
|
module ActiveRecordStubExtensions
|
@@ -136,7 +146,7 @@ EOM
|
|
136
146
|
end
|
137
147
|
|
138
148
|
def connection
|
139
|
-
raise
|
149
|
+
raise IllegalDataAccessException.new("stubbed models are not allowed to access the database")
|
140
150
|
end
|
141
151
|
end
|
142
152
|
|
@@ -187,6 +197,7 @@ EOM
|
|
187
197
|
stubs = stubs.reverse_merge(:id => next_id)
|
188
198
|
stubs = stubs.reverse_merge(:persisted? => !!stubs[:id])
|
189
199
|
end
|
200
|
+
stubs = stubs.reverse_merge(:blank? => false)
|
190
201
|
stubs.each do |k,v|
|
191
202
|
m.__send__("#{k}=", stubs.delete(k)) if m.respond_to?("#{k}=")
|
192
203
|
end
|
@@ -202,6 +213,12 @@ EOM
|
|
202
213
|
def next_id
|
203
214
|
@@model_id += 1
|
204
215
|
end
|
216
|
+
|
205
217
|
end
|
206
218
|
end
|
207
219
|
end
|
220
|
+
|
221
|
+
RSpec.configure do |config|
|
222
|
+
config.mock_with :mocha
|
223
|
+
config.include RSpec::Rails::Mocha
|
224
|
+
end
|
data/spec/mocha_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'rspec'
|
2
|
-
require 'rspec
|
2
|
+
require 'rspec/rails/mocha'
|
3
3
|
|
4
4
|
# Simple stub for ActiveRecord::Base
|
5
5
|
module ActiveRecord
|
@@ -16,9 +16,6 @@ module RSpec::Rails
|
|
16
16
|
class IllegalDataAccessException < StandardError; end
|
17
17
|
end
|
18
18
|
|
19
|
-
require 'active_support/core_ext/hash/reverse_merge'
|
20
|
-
require 'active_support/core_ext/object'
|
21
|
-
|
22
19
|
class Person < ActiveRecord::Base
|
23
20
|
attr_accessor :id, :name
|
24
21
|
|
@@ -59,13 +56,6 @@ describe "rspec-rails Mocha plugin" do
|
|
59
56
|
person.id.should == 66
|
60
57
|
end
|
61
58
|
|
62
|
-
it "should use given ID in to_key" do
|
63
|
-
personA = create(Person, :id => 66)
|
64
|
-
personA.to_key.should == [66]
|
65
|
-
personB = create(Person)
|
66
|
-
personB.to_key.first.should >= 1000
|
67
|
-
end
|
68
|
-
|
69
59
|
it "should mock a record with properties" do
|
70
60
|
hacker = create(Hacker, :name => "Mislav", :skillz => 1337)
|
71
61
|
hacker.name.should == "Mislav"
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-rails-mocha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
4
|
+
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
8
|
+
- 1
|
9
|
+
version: 0.3.1
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- "Mislav Marohni\xC4\x87"
|
@@ -16,7 +15,7 @@ autorequire:
|
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
17
|
|
19
|
-
date: 2011-
|
18
|
+
date: 2011-06-18 00:00:00 +02:00
|
20
19
|
default_executable:
|
21
20
|
dependencies:
|
22
21
|
- !ruby/object:Gem::Dependency
|
@@ -27,7 +26,6 @@ dependencies:
|
|
27
26
|
requirements:
|
28
27
|
- - ">="
|
29
28
|
- !ruby/object:Gem::Version
|
30
|
-
hash: 43
|
31
29
|
segments:
|
32
30
|
- 0
|
33
31
|
- 9
|
@@ -43,7 +41,6 @@ dependencies:
|
|
43
41
|
requirements:
|
44
42
|
- - ">="
|
45
43
|
- !ruby/object:Gem::Version
|
46
|
-
hash: 15
|
47
44
|
segments:
|
48
45
|
- 2
|
49
46
|
- 0
|
@@ -62,10 +59,9 @@ extra_rdoc_files: []
|
|
62
59
|
files:
|
63
60
|
- Rakefile
|
64
61
|
- lib/rspec/rails/mocha.rb
|
65
|
-
- lib/rspec-rails-mocha.rb
|
66
62
|
- spec/mocha_spec.rb
|
67
63
|
- README.markdown
|
68
|
-
has_rdoc:
|
64
|
+
has_rdoc: true
|
69
65
|
homepage: http://github.com/mislav/rspec-rails-mocha
|
70
66
|
licenses: []
|
71
67
|
|
@@ -79,7 +75,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
75
|
requirements:
|
80
76
|
- - ">="
|
81
77
|
- !ruby/object:Gem::Version
|
82
|
-
hash: 3
|
83
78
|
segments:
|
84
79
|
- 0
|
85
80
|
version: "0"
|
@@ -88,16 +83,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
83
|
requirements:
|
89
84
|
- - ">="
|
90
85
|
- !ruby/object:Gem::Version
|
91
|
-
hash: 3
|
92
86
|
segments:
|
93
87
|
- 0
|
94
88
|
version: "0"
|
95
89
|
requirements: []
|
96
90
|
|
97
91
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.
|
92
|
+
rubygems_version: 1.3.7
|
99
93
|
signing_key:
|
100
94
|
specification_version: 3
|
101
|
-
summary: mock_model and stub_model
|
95
|
+
summary: Mocha versions of mock_model and stub_model from rspec-rails
|
102
96
|
test_files: []
|
103
97
|
|