Skip to content

Instantly share code, notes, and snippets.

@coffeejunk
Created April 12, 2012 16:15
Show Gist options
  • Save coffeejunk/2368754 to your computer and use it in GitHub Desktop.
Save coffeejunk/2368754 to your computer and use it in GitHub Desktop.
flexmock error when mocking methods created by method_missing & respond_to_missing?
# slight modification of the example 'Creating Partial Mocks' from http://flexmock.rubyforge.org/
require 'test/unit'
require 'flexmock/test_unit'
class Woofer
def woof
'miau'
end
end
class Dog
def initialize
@woofer = Woofer.new
end
def wag
:happy
end
def method_missing(method, *args, &block)
if method.to_s =~ /bark/
@woofer.woof
else
super
end
end
def respond_to_missing?(method, *)
method =~ /bark/ || super
end
end
class TestDogBarking < Test::Unit::TestCase
include FlexMock::TestCase
# Setup the tests by mocking the +new+ method of
# Woofer and return a mock woofer.
def setup
@dog = Dog.new
flexmock(@dog, :bark => :grrr)
end
def test_dog
assert_equal :grrr, @dog.bark # Mocked Method
assert_equal :happy, @dog.wag # Normal Method
end
end
[ruby-1.9.2-p318]$ ruby dog_test.rb
Loaded suite dog_test
Started
E
Finished in 0.001032 seconds.
1) Error:
test_dog(TestDogBarking):
NameError: undefined method `flexmock_original_behavior_for_bark' for class `Dog'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/partial_mock.rb:280:in `alias_method'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/partial_mock.rb:280:in `block in restore_original_definition'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/partial_mock.rb:279:in `class_eval'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/partial_mock.rb:279:in `restore_original_definition'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/partial_mock.rb:162:in `block in flexmock_teardown'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/partial_mock.rb:160:in `each'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/partial_mock.rb:160:in `flexmock_teardown'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/mock_container.rb:53:in `block in flexmock_close'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/mock_container.rb:52:in `each'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/mock_container.rb:52:in `flexmock_close'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/mock_container.rb:34:in `flexmock_teardown'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/test_unit.rb:26:in `teardown'
/Users/max/.rvm/gems/ruby-1.9.2-p318/gems/flexmock-0.9.0/lib/flexmock/test_unit_integration.rb:36:in `teardown'
1 tests, 2 assertions, 0 failures, 1 errors, 0 skips
Test run options: --seed 54814
# lib/flexmock/partial_mock.rb
#
# workaround for the problem:
# ignore NameErrors the same way create_alias_for_existing_method(method_name) does:
def restore_original_definition(method_name)
begin
method_def = @method_definitions[method_name]
if method_def
the_alias = new_name(method_name)
sclass.class_eval do
alias_method(method_name, the_alias)
end
end
rescue NameError => _
# Alias attempt failed
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment