Skip to content

Commit 02d7722

Browse files
committed
Uses newly available syntax-agnostic APIs for setting up stubs
[Closes #764]
1 parent d546278 commit 02d7722

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

lib/rspec/rails/mocks.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module ActiveModelInstanceMethods
1212
# Stubs `persisted?` to return false and `id` to return nil
1313
# @return self
1414
def as_new_record
15-
self.stub(:persisted?) { false }
16-
self.stub(:id) { nil }
15+
RSpec::Mocks.allow_message(self, :persisted?).and_return(false)
16+
RSpec::Mocks.allow_message(self, :id).and_return(nil)
1717
self
1818
end
1919

@@ -32,8 +32,8 @@ def respond_to?(message, include_private=false)
3232
module ActiveRecordInstanceMethods
3333
# Stubs `persisted?` to return `false` and `id` to return `nil`.
3434
def destroy
35-
self.stub(:persisted?) { false }
36-
self.stub(:id) { nil }
35+
RSpec::Mocks.allow_message(self, :persisted?).and_return(false)
36+
RSpec::Mocks.allow_message(self, :id).and_return(nil)
3737
end
3838

3939
# Transforms the key to a method and calls it.
@@ -106,7 +106,7 @@ def self.primary_key; :id; end
106106
if defined?(ActiveRecord)
107107
[:save, :update_attributes, :update].each do |key|
108108
if stubs[key] == false
109-
m.errors.stub(:empty? => false)
109+
RSpec::Mocks.allow_message(m.errors, :empty?).and_return(false)
110110
end
111111
end
112112
end
@@ -150,8 +150,8 @@ def @object.to_s
150150
module ActiveModelStubExtensions
151151
# Stubs `persisted` to return false and `id` to return nil
152152
def as_new_record
153-
self.stub(:persisted?) { false }
154-
self.stub(:id) { nil }
153+
RSpec::Mocks.allow_message(self, :persisted?).and_return(false)
154+
RSpec::Mocks.allow_message(self, :id).and_return(nil)
155155
self
156156
end
157157

@@ -223,7 +223,11 @@ def stub_model(model_class, stubs={})
223223
stubs.each do |k,v|
224224
m.__send__("#{k}=", stubs.delete(k)) if m.respond_to?("#{k}=")
225225
end
226-
m.stub(stubs)
226+
227+
stubs.each do |message, return_val|
228+
RSpec::Mocks.allow_message(m, message).and_return(return_val)
229+
end
230+
227231
yield m if block_given?
228232
end
229233
end

0 commit comments

Comments
 (0)