Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from freerange/prevent-use-of-mocha-outside-test
Prevent use of Mocha outside the context of a test/example
- Loading branch information
Showing
26 changed files
with
206 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'mocha/backtrace_filter' | ||
|
||
module Mocha | ||
|
||
# @private | ||
class ErrorWithFilteredBacktrace < StandardError | ||
|
||
# @private | ||
def initialize(message = nil, backtrace = []) | ||
super(message) | ||
filter = BacktraceFilter.new | ||
set_backtrace(filter.filtered(backtrace)) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'mocha/error_with_filtered_backtrace' | ||
|
||
module Mocha | ||
|
||
# Exception raised when Mocha has not been initialized, e.g. outside the | ||
# context of a test. | ||
class NotInitializedError < ErrorWithFilteredBacktrace; end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,10 @@ | ||
require 'mocha/backtrace_filter' | ||
require 'mocha/error_with_filtered_backtrace' | ||
|
||
module Mocha | ||
|
||
# Exception raised when stubbing a particular method is not allowed. | ||
# | ||
# @see Configuration.prevent | ||
class StubbingError < StandardError | ||
|
||
# @private | ||
def initialize(message = nil, backtrace = []) | ||
super(message) | ||
filter = BacktraceFilter.new | ||
set_backtrace(filter.filtered(backtrace)) | ||
end | ||
|
||
end | ||
class StubbingError < ErrorWithFilteredBacktrace; end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require File.expand_path('../acceptance_test_helper', __FILE__) | ||
require 'mocha/setup' | ||
require 'mocha/not_initialized_error' | ||
|
||
class PreventUseOfMochaOutsideTestTest < Mocha::TestCase | ||
|
||
include AcceptanceTest | ||
|
||
def setup | ||
setup_acceptance_test | ||
mocha_teardown | ||
end | ||
|
||
def teardown | ||
teardown_acceptance_test | ||
end | ||
|
||
def test_should_raise_exception_when_mock_called_outside_test | ||
assert_raises(Mocha::NotInitializedError) { mock('object') } | ||
end | ||
|
||
def test_should_raise_exception_when_stub_called_outside_test | ||
assert_raises(Mocha::NotInitializedError) { stub('object') } | ||
end | ||
|
||
def test_should_raise_exception_when_stub_everything_called_outside_test | ||
assert_raises(Mocha::NotInitializedError) { stub_everything('object') } | ||
end | ||
|
||
def test_should_raise_exception_when_states_called_outside_test | ||
assert_raises(Mocha::NotInitializedError) { states('state-machine') } | ||
end | ||
|
||
def test_should_raise_exception_when_expects_called_on_instance_outside_test | ||
instance = Class.new.new | ||
assert_raises(Mocha::NotInitializedError) { instance.expects(:expected_method) } | ||
end | ||
|
||
def test_should_raise_exception_when_expects_called_on_class_outside_test | ||
klass = Class.new | ||
assert_raises(Mocha::NotInitializedError) { klass.expects(:expected_method) } | ||
end | ||
|
||
def test_should_raise_exception_when_expects_called_on_any_instance_outside_test | ||
klass = Class.new | ||
assert_raises(Mocha::NotInitializedError) { klass.any_instance.expects(:expected_method) } | ||
end | ||
|
||
def test_should_raise_exception_when_stubs_called_on_instance_outside_test | ||
instance = Class.new.new | ||
assert_raises(Mocha::NotInitializedError) { instance.stubs(:expected_method) } | ||
end | ||
|
||
def test_should_raise_exception_when_stubs_called_on_class_outside_test | ||
klass = Class.new | ||
assert_raises(Mocha::NotInitializedError) { klass.stubs(:expected_method) } | ||
end | ||
|
||
def test_should_raise_exception_when_stubs_called_on_any_instance_outside_test | ||
klass = Class.new | ||
assert_raises(Mocha::NotInitializedError) { klass.any_instance.stubs(:expected_method) } | ||
end | ||
|
||
def test_should_raise_exception_when_unstub_called_on_instance_outside_test | ||
instance = Class.new.new | ||
assert_raises(Mocha::NotInitializedError) { instance.unstub(:expected_method) } | ||
end | ||
|
||
def test_should_raise_exception_when_unstub_called_on_class_outside_test | ||
klass = Class.new | ||
assert_raises(Mocha::NotInitializedError) { klass.unstub(:expected_method) } | ||
end | ||
|
||
def test_should_raise_exception_when_unstub_called_on_any_instance_outside_test | ||
klass = Class.new | ||
assert_raises(Mocha::NotInitializedError) { klass.any_instance.unstub(:expected_method) } | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.