Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fail fast if mocking before setup #319

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mocha/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Hooks
#
# This method should be called before each individual test starts (including before any "setup" code).
def mocha_setup
Mockery.build_instance
end

# Verifies that all mock expectations have been met (only for use by authors of test libraries).
Expand Down
27 changes: 24 additions & 3 deletions lib/mocha/mockery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,30 @@
module Mocha

class Mockery
class Null
def method_missing(method_name, *args, &block)
if method_name == :raise
super
else
raise StubbingError.new('Mocking/stubbing outside of the per-test lifecycle is not supported.', caller)
end
end

def respond_to_missing?(method_name, include_private = false)
(method != :raise) || super
end

# No ops - just to make acceptance tests pass
def verify(*args); end
def teardown; end
attr_writer :logger
end

class << self
attr_reader :instance

def instance
@instance ||= new
def build_instance
@instance = new
end

def verify(*args)
Expand All @@ -28,11 +47,13 @@ def teardown
end

def reset_instance
@instance = nil
@instance = Null.new
end

end

reset_instance

def named_mock(name, &block)
add_mock(Mock.new(self, Name.new(name), &block))
end
Expand Down
8 changes: 8 additions & 0 deletions test/unit/mockery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ class MockeryTest < Mocha::TestCase
include Mocha
include DeprecationDisabler

def test_should_return_null_mockery_if_not_built
mockery = Mockery.reset_instance
mockery = Mockery.instance
assert_not_nil mockery
assert_kind_of Mockery::Null, mockery
end

def test_should_build_instance_of_mockery
Mockery.build_instance
mockery = Mockery.instance
assert_not_nil mockery
assert_kind_of Mockery, mockery
Expand Down
2 changes: 2 additions & 0 deletions test/unit/object_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
require 'mocha/object_methods'
require 'mocha/mock'
require 'mocha/expectation_error_factory'
require 'mocha/mockery'

class ObjectMethodsTest < Mocha::TestCase

def setup
Mocha::Mockery.build_instance
@object = Object.new.extend(Mocha::ObjectMethods)
end

Expand Down