diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2015-08-13 09:56:38 +0200 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2015-08-13 09:56:38 +0200 |
commit | 0650d0848817bbc71d1450297b066445e8433ff1 (patch) | |
tree | a5cc0e89755f9bc13d1e2945bd27833047f2290b | |
parent | 48a183ecb9992723a0b7dc8eee1c4bd41cf8d921 (diff) | |
parent | bfe75328761bcbb7933dcf7714f6049c094b62dc (diff) | |
download | rails-0650d0848817bbc71d1450297b066445e8433ff1.tar.gz rails-0650d0848817bbc71d1450297b066445e8433ff1.tar.bz2 rails-0650d0848817bbc71d1450297b066445e8433ff1.zip |
Merge pull request #21025 from ronakjangir47/asserts
Added helper methods to stub any instance
-rw-r--r-- | activesupport/lib/active_support/testing/method_call_assertions.rb | 6 | ||||
-rw-r--r-- | activesupport/test/testing/method_call_assertions_test.rb | 13 |
2 files changed, 18 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/testing/method_call_assertions.rb b/activesupport/lib/active_support/testing/method_call_assertions.rb index e3cbe40308..517f02e3ef 100644 --- a/activesupport/lib/active_support/testing/method_call_assertions.rb +++ b/activesupport/lib/active_support/testing/method_call_assertions.rb @@ -30,6 +30,10 @@ module ActiveSupport def assert_not_called(object, method_name, message = nil, &block) assert_called(object, method_name, message, times: 0, &block) end + + def stub_any_instance(klass, instance: klass.new) + klass.stub(:new, instance) { yield instance } + end end end -end
\ No newline at end of file +end diff --git a/activesupport/test/testing/method_call_assertions_test.rb b/activesupport/test/testing/method_call_assertions_test.rb index a9908aea0d..652cbda8da 100644 --- a/activesupport/test/testing/method_call_assertions_test.rb +++ b/activesupport/test/testing/method_call_assertions_test.rb @@ -95,4 +95,17 @@ class MethodCallAssertionsTest < ActiveSupport::TestCase assert_equal "Expected increment to be called 0 times, but was called 1 times.\nExpected: 0\n Actual: 1", error.message end + + def test_stub_any_instance + stub_any_instance(Level) do |instance| + assert_equal instance, Level.new + end + end + + def test_stub_any_instance_with_instance + stub_any_instance(Level, instance: @object) do |instance| + assert_equal @object, instance + assert_equal instance, Level.new + end + end end |