Skip to content

Commit

Permalink
Add test for stubbing class method defined on module & aliased
Browse files Browse the repository at this point in the history
This test was derived from the tests in #244 and was failing before we
moved to always (in Ruby v2+) using the prepended module stubbing
mechanism. We'll probably want to expand it to cover some more
scenarios, but it's useful as it stands.

This test fails in Ruby 1.8.x and we can't find an obvious fix.

The test passes in Ruby 1.9.x due to a change in behaviour of `#owner`
when a class is `extend`ed by a module.

The test passes in Rubies > 2.0 because we now always prepend a module
to avoid overwriting the original method at all.
  • Loading branch information
floehopper authored and chrisroos committed Aug 25, 2016
1 parent 43d5667 commit c3af31b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/acceptance/stub_method_defined_on_module_and_aliased_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require File.expand_path('../acceptance_test_helper', __FILE__)
require 'mocha/setup'

unless Mocha::PRE_RUBY_V19
class StubMethodDefinedOnModuleAndAliasedTest < Mocha::TestCase
include AcceptanceTest

def setup
setup_acceptance_test
end

def teardown
teardown_acceptance_test
end

def test_stubbing_class_method_defined_by_aliasing_module_instance_method
mod = Module.new do
def module_instance_method
'module-instance-method'
end
end

klass = Class.new do
extend mod
class << self
alias_method :aliased_module_instance_method, :module_instance_method
end
end

assert_snapshot_unchanged(klass) do
test_result = run_as_test do
klass.stubs(:aliased_module_instance_method).returns('stubbed-aliased-module-instance-method')
assert_equal 'stubbed-aliased-module-instance-method', klass.aliased_module_instance_method
end
assert_passed(test_result)
end
end
end
end

0 comments on commit c3af31b

Please sign in to comment.