Skip to content

Commit d37f01b

Browse files
committed
Merge pull request rails#20457 from ronakjangir47/remove_mocha_activesupport
Removed use of mocha in Active Support
2 parents 87b4f31 + 2f28e5b commit d37f01b

File tree

8 files changed

+87
-65
lines changed

8 files changed

+87
-65
lines changed

activesupport/test/abstract_unit.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
end
1616

1717
require 'active_support/testing/autorun'
18+
require 'active_support/testing/method_call_assertions'
1819

1920
ENV['NO_RELOAD'] = '1'
2021
require 'active_support'
@@ -38,4 +39,7 @@ def jruby_skip(message = '')
3839
end
3940

4041
require 'minitest/mock'
41-
require 'mocha/setup' # FIXME: stop using mocha
42+
43+
class ActiveSupport::TestCase
44+
include ActiveSupport::Testing::MethodCallAssertions
45+
end

activesupport/test/caching_test.rb

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,37 +133,42 @@ def test_file_fragment_cache_store
133133
end
134134

135135
def test_mem_cache_fragment_cache_store
136-
Dalli::Client.expects(:new).with(%w[localhost], {})
137-
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
138-
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
136+
assert_called_with(Dalli::Client, :new, [%w[localhost], {}]) do
137+
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
138+
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
139+
end
139140
end
140141

141142
def test_mem_cache_fragment_cache_store_with_given_mem_cache
142143
mem_cache = Dalli::Client.new
143-
Dalli::Client.expects(:new).never
144-
store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache
145-
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
144+
assert_not_called(Dalli::Client, :new) do
145+
store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache
146+
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
147+
end
146148
end
147149

148150
def test_mem_cache_fragment_cache_store_with_not_dalli_client
149-
Dalli::Client.expects(:new).never
150-
memcache = Object.new
151-
assert_raises(ArgumentError) do
152-
ActiveSupport::Cache.lookup_store :mem_cache_store, memcache
151+
assert_not_called(Dalli::Client, :new) do
152+
memcache = Object.new
153+
assert_raises(ArgumentError) do
154+
ActiveSupport::Cache.lookup_store :mem_cache_store, memcache
155+
end
153156
end
154157
end
155158

156159
def test_mem_cache_fragment_cache_store_with_multiple_servers
157-
Dalli::Client.expects(:new).with(%w[localhost 192.168.1.1], {})
158-
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
159-
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
160+
assert_called_with(Dalli::Client, :new, [%w[localhost 192.168.1.1], {}]) do
161+
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
162+
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
163+
end
160164
end
161165

162166
def test_mem_cache_fragment_cache_store_with_options
163-
Dalli::Client.expects(:new).with(%w[localhost 192.168.1.1], { :timeout => 10 })
164-
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo', :timeout => 10
165-
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
166-
assert_equal 'foo', store.options[:namespace]
167+
assert_called_with(Dalli::Client, :new, [%w[localhost 192.168.1.1], { :timeout => 10 }]) do
168+
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo', :timeout => 10
169+
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
170+
assert_equal 'foo', store.options[:namespace]
171+
end
167172
end
168173

169174
def test_object_assigned_fragment_cache_store
@@ -224,13 +229,15 @@ def test_should_overwrite
224229

225230
def test_fetch_without_cache_miss
226231
@cache.write('foo', 'bar')
227-
@cache.expects(:write).never
228-
assert_equal 'bar', @cache.fetch('foo') { 'baz' }
232+
assert_not_called(@cache, :write) do
233+
assert_equal 'bar', @cache.fetch('foo') { 'baz' }
234+
end
229235
end
230236

231237
def test_fetch_with_cache_miss
232-
@cache.expects(:write).with('foo', 'baz', @cache.options)
233-
assert_equal 'baz', @cache.fetch('foo') { 'baz' }
238+
assert_called_with(@cache, :write, ['foo', 'baz', @cache.options]) do
239+
assert_equal 'baz', @cache.fetch('foo') { 'baz' }
240+
end
234241
end
235242

236243
def test_fetch_with_cache_miss_passes_key_to_block
@@ -245,15 +252,18 @@ def test_fetch_with_cache_miss_passes_key_to_block
245252

246253
def test_fetch_with_forced_cache_miss
247254
@cache.write('foo', 'bar')
248-
@cache.expects(:read).never
249-
@cache.expects(:write).with('foo', 'bar', @cache.options.merge(:force => true))
250-
@cache.fetch('foo', :force => true) { 'bar' }
255+
assert_not_called(@cache, :read) do
256+
assert_called_with(@cache, :write, ['foo', 'bar', @cache.options.merge(:force => true)]) do
257+
@cache.fetch('foo', :force => true) { 'bar' }
258+
end
259+
end
251260
end
252261

253262
def test_fetch_with_cached_nil
254263
@cache.write('foo', nil)
255-
@cache.expects(:write).never
256-
assert_nil @cache.fetch('foo') { 'baz' }
264+
assert_not_called(@cache, :write) do
265+
assert_nil @cache.fetch('foo') { 'baz' }
266+
end
257267
end
258268

259269
def test_should_read_and_write_hash
@@ -304,8 +314,9 @@ def test_fetch_multi
304314
end
305315

306316
def test_multi_with_objects
307-
foo = stub(:title => 'FOO!', :cache_key => 'foo')
308-
bar = stub(:cache_key => 'bar')
317+
cache_struct = Struct.new(:cache_key, :title)
318+
foo = cache_struct.new('foo', 'FOO!')
319+
bar = cache_struct.new('bar')
309320

310321
@cache.write('bar', 'BAM!')
311322

@@ -774,9 +785,10 @@ def test_delete_does_not_delete_empty_parent_dir
774785
end
775786

776787
def test_log_exception_when_cache_read_fails
777-
File.expects(:exist?).raises(StandardError, "failed")
778-
@cache.send(:read_entry, "winston", {})
779-
assert @buffer.string.present?
788+
File.stub(:exist?, -> { raise StandardError.new("failed") }) do
789+
@cache.send(:read_entry, "winston", {})
790+
assert @buffer.string.present?
791+
end
780792
end
781793

782794
def test_cleanup_removes_all_expired_entries

activesupport/test/core_ext/hash_ext_test.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,10 +961,11 @@ def test_except_with_original_frozen
961961
assert_raise(RuntimeError) { original.except!(:a) }
962962
end
963963

964-
def test_except_with_mocha_expectation_on_original
964+
def test_except_does_not_delete_values_in_original
965965
original = { :a => 'x', :b => 'y' }
966-
original.expects(:delete).never
967-
original.except(:a)
966+
assert_not_called(original, :delete) do
967+
original.except(:a)
968+
end
968969
end
969970

970971
def test_compact

activesupport/test/core_ext/time_with_zone_test.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -482,22 +482,24 @@ def test_freeze_preloads_instance_variables
482482
end
483483

484484
def test_method_missing_with_non_time_return_value
485-
@twz.time.expects(:foo).returns('bar')
485+
time = @twz.time
486+
def time.foo; 'bar'; end
486487
assert_equal 'bar', @twz.foo
487488
end
488489

489490
def test_date_part_value_methods
490491
twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
491-
twz.expects(:method_missing).never
492-
assert_equal 1999, twz.year
493-
assert_equal 12, twz.month
494-
assert_equal 31, twz.day
495-
assert_equal 14, twz.hour
496-
assert_equal 18, twz.min
497-
assert_equal 17, twz.sec
498-
assert_equal 500, twz.usec
499-
assert_equal 5, twz.wday
500-
assert_equal 365, twz.yday
492+
assert_not_called(twz, :method_missing) do
493+
assert_equal 1999, twz.year
494+
assert_equal 12, twz.month
495+
assert_equal 31, twz.day
496+
assert_equal 14, twz.hour
497+
assert_equal 18, twz.min
498+
assert_equal 17, twz.sec
499+
assert_equal 500, twz.usec
500+
assert_equal 5, twz.wday
501+
assert_equal 365, twz.yday
502+
end
501503
end
502504

503505
def test_usec_returns_0_when_datetime_is_wrapped

activesupport/test/dependencies_test.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,13 @@ def test_unloadable_should_return_change_flag
721721
end
722722

723723
def test_unloadable_constants_should_receive_callback
724-
Object.const_set :C, Class.new
724+
Object.const_set :C, Class.new { def self.before_remove_const; end }
725725
C.unloadable
726-
C.expects(:before_remove_const).once
727-
assert C.respond_to?(:before_remove_const)
728-
ActiveSupport::Dependencies.clear
729-
assert !defined?(C)
726+
assert_called(C, :before_remove_const, times: 1) do
727+
assert C.respond_to?(:before_remove_const)
728+
ActiveSupport::Dependencies.clear
729+
assert !defined?(C)
730+
end
730731
ensure
731732
remove_constants(:C)
732733
end

activesupport/test/deprecation_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ def method
256256
end
257257

258258
def test_deprecate_with_custom_deprecator
259-
custom_deprecator = mock('Deprecator') do
260-
expects(:deprecation_warning)
261-
end
259+
custom_deprecator = Struct.new(:deprecation_warning).new
262260

263-
klass = Class.new do
264-
def method
261+
assert_called_with(custom_deprecator, :deprecation_warning, [:method, nil]) do
262+
klass = Class.new do
263+
def method
264+
end
265+
deprecate :method, deprecator: custom_deprecator
265266
end
266-
deprecate :method, deprecator: custom_deprecator
267-
end
268267

269-
klass.new.method
268+
klass.new.method
269+
end
270270
end
271271

272272
def test_deprecated_constant_with_deprecator_given

activesupport/test/log_subscriber_test.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ def test_does_not_send_the_event_if_it_doesnt_match_the_class
8282

8383
def test_does_not_send_the_event_if_logger_is_nil
8484
ActiveSupport::LogSubscriber.logger = nil
85-
@log_subscriber.expects(:some_event).never
86-
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
87-
instrument "some_event.my_log_subscriber"
88-
wait
85+
assert_not_called(@log_subscriber, :some_event) do
86+
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
87+
instrument "some_event.my_log_subscriber"
88+
wait
89+
end
8990
end
9091

9192
def test_does_not_fail_with_non_namespaced_events

activesupport/test/multibyte_unicode_database_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ def setup
1111

1212
UnicodeDatabase::ATTRIBUTES.each do |attribute|
1313
define_method "test_lazy_loading_on_attribute_access_of_#{attribute}" do
14-
@ucd.expects(:load)
15-
@ucd.send(attribute)
14+
assert_called(@ucd, :load) do
15+
@ucd.send(attribute)
16+
end
1617
end
1718
end
1819

0 commit comments

Comments
 (0)