mocha 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/RELEASE.md +4 -0
  3. data/lib/mocha/central.rb +17 -0
  4. data/lib/mocha/error_with_filtered_backtrace.rb +17 -0
  5. data/lib/mocha/hooks.rb +1 -2
  6. data/lib/mocha/integration/mini_test/version_13.rb +1 -0
  7. data/lib/mocha/integration/mini_test/version_140.rb +1 -0
  8. data/lib/mocha/integration/mini_test/version_141.rb +1 -0
  9. data/lib/mocha/integration/mini_test/version_142_to_172.rb +1 -0
  10. data/lib/mocha/integration/mini_test/version_200.rb +1 -0
  11. data/lib/mocha/integration/mini_test/version_201_to_222.rb +1 -0
  12. data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +1 -0
  13. data/lib/mocha/integration/mini_test/version_2112_to_320.rb +1 -0
  14. data/lib/mocha/integration/mini_test/version_230_to_2101.rb +1 -0
  15. data/lib/mocha/integration/test_unit/gem_version_200.rb +1 -0
  16. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +1 -0
  17. data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +1 -0
  18. data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +1 -0
  19. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +1 -0
  20. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +1 -0
  21. data/lib/mocha/mockery.rb +38 -3
  22. data/lib/mocha/not_initialized_error.rb +9 -0
  23. data/lib/mocha/stubbing_error.rb +2 -11
  24. data/lib/mocha/version.rb +1 -1
  25. data/test/acceptance/acceptance_test_helper.rb +0 -2
  26. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +79 -0
  27. data/test/unit/hooks_test.rb +5 -4
  28. data/test/unit/mockery_test.rb +17 -2
  29. data/test/unit/object_methods_test.rb +6 -0
  30. metadata +5 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1a4df2eb13604de6b309bc453307d375c055620
4
- data.tar.gz: 9144ebfe812beea562fc8c9ebdfb87326cb5ffa6
3
+ metadata.gz: ae952b50093776beef6c7083184899e6875738d7
4
+ data.tar.gz: b45fc4c6fe7d7ab128a7227f3fdf2db01e349989
5
5
  SHA512:
6
- metadata.gz: 1cf5b923bb4a91c6b3a1dac90aca3084229955743ae64c41cbecf561e8fd572f419d09d155ff3c9b6d0616258ca526d70677d79f9861e8987f99ee5b312bbb90
7
- data.tar.gz: 9e635e0f0897d0437acffe0cfe4755ecae028da76513445a5b4b56ee48be878ae0070fa24d3fc8bddad02a69d790223909b936fdcdb6f53114feeec8c69cd3c4
6
+ metadata.gz: c5ca31997f0e6342f4d757a4a42da220cbc51878b878b48f61b4d74b095f7347a846c6196f65568c4dde737c8ccbe13e15eba3fce25054d5232724535021a1b3
7
+ data.tar.gz: b8797e8b7e3a58b31e3f773860989aafc85fabd2cbf4529a965b521e7ec1f995304b26186f49062c6ec8af6b8b4749730e89373ebf1e909df1b7e7fb5ca793a1
data/RELEASE.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Release Notes
2
2
 
3
+ ## 1.5.0
4
+
5
+ * Prevent use of Mocha outside the context of a test/example - thanks to @andyw8 & @lzap (#327)
6
+
3
7
  ## 1.4.0
4
8
 
5
9
  * Fix deprecation warning for `assert_nil` in `ClassMethodTest` (#308 & #309)
@@ -2,6 +2,23 @@ module Mocha
2
2
 
3
3
  class Central
4
4
 
5
+ class Null < self
6
+
7
+ def initialize(&block)
8
+ super
9
+ @raise_not_initialized_error = block
10
+ end
11
+
12
+ def stub(*)
13
+ @raise_not_initialized_error.call
14
+ end
15
+
16
+ def unstub(*)
17
+ @raise_not_initialized_error.call
18
+ end
19
+
20
+ end
21
+
5
22
  attr_accessor :stubba_methods
6
23
 
7
24
  def initialize
@@ -0,0 +1,17 @@
1
+ require 'mocha/backtrace_filter'
2
+
3
+ module Mocha
4
+
5
+ # @private
6
+ class ErrorWithFilteredBacktrace < StandardError
7
+
8
+ # @private
9
+ def initialize(message = nil, backtrace = [])
10
+ super(message)
11
+ filter = BacktraceFilter.new
12
+ set_backtrace(filter.filtered(backtrace))
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -21,6 +21,7 @@ module Mocha
21
21
  #
22
22
  # This method should be called before each individual test starts (including before any "setup" code).
23
23
  def mocha_setup
24
+ Mockery.setup
24
25
  end
25
26
 
26
27
  # Verifies that all mock expectations have been met (only for use by authors of test libraries).
@@ -37,8 +38,6 @@ module Mocha
37
38
  # This method should be called after each individual test has finished (including after any "teardown" code).
38
39
  def mocha_teardown
39
40
  Mockery.teardown
40
- ensure
41
- Mockery.reset_instance
42
41
  end
43
42
  end
44
43
  end
@@ -25,6 +25,7 @@ module Mocha
25
25
  begin
26
26
  begin
27
27
  @passed = nil
28
+ mocha_setup
28
29
  self.setup
29
30
  self.__send__ self.name
30
31
  mocha_verify(assertion_counter)
@@ -25,6 +25,7 @@ module Mocha
25
25
  begin
26
26
  begin
27
27
  @passed = nil
28
+ mocha_setup
28
29
  self.setup
29
30
  self.__send__ self.__name__
30
31
  mocha_verify(assertion_counter)
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.__send__ self.__name__
36
37
  mocha_verify(assertion_counter)
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.__send__ self.__name__
36
37
  mocha_verify(assertion_counter)
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.__send__ self.__name__
36
37
  mocha_verify(assertion_counter)
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.__send__ self.__name__
36
37
  mocha_verify(assertion_counter)
@@ -32,6 +32,7 @@ module Mocha
32
32
  begin
33
33
  @passed = nil
34
34
  self.before_setup
35
+ mocha_setup
35
36
  self.setup
36
37
  self.after_setup
37
38
  self.run_test self.__name__
@@ -35,6 +35,7 @@ module Mocha
35
35
  begin
36
36
  @passed = nil
37
37
  self.before_setup
38
+ mocha_setup
38
39
  self.setup
39
40
  self.after_setup
40
41
  self.run_test self.__name__
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.run_setup_hooks
36
37
  self.__send__ self.__name__
@@ -26,6 +26,7 @@ module Mocha
26
26
  yield(Test::Unit::TestCase::STARTED, name)
27
27
  begin
28
28
  begin
29
+ mocha_setup
29
30
  run_setup
30
31
  __send__(@method_name)
31
32
  mocha_verify(assertion_counter)
@@ -26,6 +26,7 @@ module Mocha
26
26
  yield(Test::Unit::TestCase::STARTED, name)
27
27
  begin
28
28
  begin
29
+ mocha_setup
29
30
  run_setup
30
31
  run_test
31
32
  mocha_verify(assertion_counter)
@@ -26,6 +26,7 @@ module Mocha
26
26
  yield(Test::Unit::TestCase::STARTED, name)
27
27
  begin
28
28
  begin
29
+ mocha_setup
29
30
  run_setup
30
31
  run_test
31
32
  mocha_verify(assertion_counter)
@@ -28,6 +28,7 @@ module Mocha
28
28
  yield(Test::Unit::TestCase::STARTED_OBJECT, self)
29
29
  begin
30
30
  begin
31
+ mocha_setup
31
32
  run_setup
32
33
  run_test
33
34
  run_cleanup
@@ -25,6 +25,7 @@ module Mocha
25
25
  @_result = result
26
26
  begin
27
27
  begin
28
+ mocha_setup
28
29
  setup
29
30
  __send__(@method_name)
30
31
  mocha_verify(assertion_counter)
@@ -25,6 +25,7 @@ module Mocha
25
25
  @_result = result
26
26
  begin
27
27
  begin
28
+ mocha_setup
28
29
  setup
29
30
  __send__(@method_name)
30
31
  mocha_verify(assertion_counter)
@@ -7,16 +7,46 @@ require 'mocha/state_machine'
7
7
  require 'mocha/logger'
8
8
  require 'mocha/configuration'
9
9
  require 'mocha/stubbing_error'
10
+ require 'mocha/not_initialized_error'
10
11
  require 'mocha/expectation_error_factory'
11
12
 
12
13
  module Mocha
13
14
 
14
15
  class Mockery
15
16
 
17
+ class Null < self
18
+
19
+ def add_mock(*)
20
+ raise_not_initialized_error
21
+ end
22
+
23
+ def add_state_machine(*)
24
+ raise_not_initialized_error
25
+ end
26
+
27
+ def stubba
28
+ Central::Null.new(&method(:raise_not_initialized_error))
29
+ end
30
+
31
+ private
32
+
33
+ def raise_not_initialized_error
34
+ message = 'Mocha methods cannot be used outside the context of a test'
35
+ raise NotInitializedError.new(message, caller)
36
+ end
37
+
38
+ end
39
+
16
40
  class << self
17
41
 
18
42
  def instance
19
- @instance ||= new
43
+ instances.last || Null.new
44
+ end
45
+
46
+ def setup
47
+ mockery = new
48
+ mockery.logger = instance.logger unless instances.empty?
49
+ @instances.push(mockery)
20
50
  end
21
51
 
22
52
  def verify(*args)
@@ -25,10 +55,15 @@ module Mocha
25
55
 
26
56
  def teardown
27
57
  instance.teardown
58
+ ensure
59
+ @instances.pop
60
+ @instances = nil if instances.empty?
28
61
  end
29
62
 
30
- def reset_instance
31
- @instance = nil
63
+ private
64
+
65
+ def instances
66
+ @instances ||= []
32
67
  end
33
68
 
34
69
  end
@@ -0,0 +1,9 @@
1
+ require 'mocha/error_with_filtered_backtrace'
2
+
3
+ module Mocha
4
+
5
+ # Exception raised when Mocha has not been initialized, e.g. outside the
6
+ # context of a test.
7
+ class NotInitializedError < ErrorWithFilteredBacktrace; end
8
+
9
+ end
@@ -1,19 +1,10 @@
1
- require 'mocha/backtrace_filter'
1
+ require 'mocha/error_with_filtered_backtrace'
2
2
 
3
3
  module Mocha
4
4
 
5
5
  # Exception raised when stubbing a particular method is not allowed.
6
6
  #
7
7
  # @see Configuration.prevent
8
- class StubbingError < StandardError
9
-
10
- # @private
11
- def initialize(message = nil, backtrace = [])
12
- super(message)
13
- filter = BacktraceFilter.new
14
- set_backtrace(filter.filtered(backtrace))
15
- end
16
-
17
- end
8
+ class StubbingError < ErrorWithFilteredBacktrace; end
18
9
 
19
10
  end
@@ -1,3 +1,3 @@
1
1
  module Mocha
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -27,13 +27,11 @@ module AcceptanceTest
27
27
  Mocha::Configuration.reset_configuration
28
28
  @logger = FakeLogger.new
29
29
  mockery = Mocha::Mockery.instance
30
- @original_logger = mockery.logger
31
30
  mockery.logger = @logger
32
31
  end
33
32
 
34
33
  def teardown_acceptance_test
35
34
  Mocha::Configuration.reset_configuration
36
- Mocha::Mockery.instance.logger = @original_logger
37
35
  end
38
36
 
39
37
  include Introspection::Assertions
@@ -0,0 +1,79 @@
1
+ require File.expand_path('../acceptance_test_helper', __FILE__)
2
+ require 'mocha/setup'
3
+ require 'mocha/not_initialized_error'
4
+
5
+ class PreventUseOfMochaOutsideTestTest < Mocha::TestCase
6
+
7
+ include AcceptanceTest
8
+
9
+ def setup
10
+ setup_acceptance_test
11
+ mocha_teardown
12
+ end
13
+
14
+ def teardown
15
+ teardown_acceptance_test
16
+ end
17
+
18
+ def test_should_raise_exception_when_mock_called_outside_test
19
+ assert_raises(Mocha::NotInitializedError) { mock('object') }
20
+ end
21
+
22
+ def test_should_raise_exception_when_stub_called_outside_test
23
+ assert_raises(Mocha::NotInitializedError) { stub('object') }
24
+ end
25
+
26
+ def test_should_raise_exception_when_stub_everything_called_outside_test
27
+ assert_raises(Mocha::NotInitializedError) { stub_everything('object') }
28
+ end
29
+
30
+ def test_should_raise_exception_when_states_called_outside_test
31
+ assert_raises(Mocha::NotInitializedError) { states('state-machine') }
32
+ end
33
+
34
+ def test_should_raise_exception_when_expects_called_on_instance_outside_test
35
+ instance = Class.new.new
36
+ assert_raises(Mocha::NotInitializedError) { instance.expects(:expected_method) }
37
+ end
38
+
39
+ def test_should_raise_exception_when_expects_called_on_class_outside_test
40
+ klass = Class.new
41
+ assert_raises(Mocha::NotInitializedError) { klass.expects(:expected_method) }
42
+ end
43
+
44
+ def test_should_raise_exception_when_expects_called_on_any_instance_outside_test
45
+ klass = Class.new
46
+ assert_raises(Mocha::NotInitializedError) { klass.any_instance.expects(:expected_method) }
47
+ end
48
+
49
+ def test_should_raise_exception_when_stubs_called_on_instance_outside_test
50
+ instance = Class.new.new
51
+ assert_raises(Mocha::NotInitializedError) { instance.stubs(:expected_method) }
52
+ end
53
+
54
+ def test_should_raise_exception_when_stubs_called_on_class_outside_test
55
+ klass = Class.new
56
+ assert_raises(Mocha::NotInitializedError) { klass.stubs(:expected_method) }
57
+ end
58
+
59
+ def test_should_raise_exception_when_stubs_called_on_any_instance_outside_test
60
+ klass = Class.new
61
+ assert_raises(Mocha::NotInitializedError) { klass.any_instance.stubs(:expected_method) }
62
+ end
63
+
64
+ def test_should_raise_exception_when_unstub_called_on_instance_outside_test
65
+ instance = Class.new.new
66
+ assert_raises(Mocha::NotInitializedError) { instance.unstub(:expected_method) }
67
+ end
68
+
69
+ def test_should_raise_exception_when_unstub_called_on_class_outside_test
70
+ klass = Class.new
71
+ assert_raises(Mocha::NotInitializedError) { klass.unstub(:expected_method) }
72
+ end
73
+
74
+ def test_should_raise_exception_when_unstub_called_on_any_instance_outside_test
75
+ klass = Class.new
76
+ assert_raises(Mocha::NotInitializedError) { klass.any_instance.unstub(:expected_method) }
77
+ end
78
+
79
+ end
@@ -1,10 +1,11 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/hooks'
3
+ require 'mocha/mockery'
3
4
 
4
5
  class HooksTest < Mocha::TestCase
5
6
  class Mocha::Mockery
6
7
  class << self
7
- attr_writer :instance
8
+ attr_writer :instances
8
9
  end
9
10
  end
10
11
 
@@ -19,11 +20,11 @@ class HooksTest < Mocha::TestCase
19
20
 
20
21
  def test_ensure_mockery_instance_is_reset_even_when_an_exception_is_raised_in_mockery_teardown
21
22
  fake_test_case = Object.new.extend(Mocha::Hooks)
22
- original_mockery = FakeMockery.new
23
- Mocha::Mockery.instance = original_mockery
23
+ mockery = FakeMockery.new
24
+ Mocha::Mockery.instances = [mockery]
24
25
 
25
26
  fake_test_case.mocha_teardown rescue nil
26
27
 
27
- assert_not_same Mocha::Mockery.instance, original_mockery
28
+ assert_kind_of Mocha::Mockery::Null, Mocha::Mockery.instance
28
29
  end
29
30
  end
@@ -9,7 +9,22 @@ class MockeryTest < Mocha::TestCase
9
9
  include Mocha
10
10
  include DeprecationDisabler
11
11
 
12
- def test_should_build_instance_of_mockery
12
+ def setup
13
+ Mockery.setup
14
+ end
15
+
16
+ def teardown
17
+ Mockery.teardown
18
+ end
19
+
20
+ def test_should_return_null_mockery_if_not_setup
21
+ Mockery.teardown
22
+ mockery = Mockery.instance
23
+ assert_not_nil mockery
24
+ assert_kind_of Mockery::Null, mockery
25
+ end
26
+
27
+ def test_should_return_instance_of_mockery
13
28
  mockery = Mockery.instance
14
29
  assert_not_nil mockery
15
30
  assert_kind_of Mockery, mockery
@@ -23,7 +38,7 @@ class MockeryTest < Mocha::TestCase
23
38
 
24
39
  def test_should_expire_mockery_instance_cache
25
40
  mockery_1 = Mockery.instance
26
- Mockery.reset_instance
41
+ Mockery.teardown
27
42
  mockery_2 = Mockery.instance
28
43
  assert_not_same mockery_1, mockery_2
29
44
  end
@@ -1,14 +1,20 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/object_methods'
3
+ require 'mocha/mockery'
3
4
  require 'mocha/mock'
4
5
  require 'mocha/expectation_error_factory'
5
6
 
6
7
  class ObjectMethodsTest < Mocha::TestCase
7
8
 
8
9
  def setup
10
+ Mocha::Mockery.setup
9
11
  @object = Object.new.extend(Mocha::ObjectMethods)
10
12
  end
11
13
 
14
+ def teardown
15
+ Mocha::Mockery.teardown
16
+ end
17
+
12
18
  def test_should_build_mocha_referring_to_self
13
19
  mocha = @object.mocha
14
20
  assert_not_nil mocha
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mocha
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mead
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-20 00:00:00.000000000 Z
11
+ date: 2018-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: metaclass
@@ -141,6 +141,7 @@ files:
141
141
  - lib/mocha/deprecation.rb
142
142
  - lib/mocha/detection/mini_test.rb
143
143
  - lib/mocha/detection/test_unit.rb
144
+ - lib/mocha/error_with_filtered_backtrace.rb
144
145
  - lib/mocha/exception_raiser.rb
145
146
  - lib/mocha/expectation.rb
146
147
  - lib/mocha/expectation_error.rb
@@ -187,6 +188,7 @@ files:
187
188
  - lib/mocha/multiple_yields.rb
188
189
  - lib/mocha/names.rb
189
190
  - lib/mocha/no_yields.rb
191
+ - lib/mocha/not_initialized_error.rb
190
192
  - lib/mocha/object_methods.rb
191
193
  - lib/mocha/parameter_matchers.rb
192
194
  - lib/mocha/parameter_matchers/all_of.rb
@@ -250,6 +252,7 @@ files:
250
252
  - test/acceptance/parameter_matcher_test.rb
251
253
  - test/acceptance/partial_mocks_test.rb
252
254
  - test/acceptance/prepend_test.rb
255
+ - test/acceptance/prevent_use_of_mocha_outside_test_test.rb
253
256
  - test/acceptance/raise_exception_test.rb
254
257
  - test/acceptance/return_value_test.rb
255
258
  - test/acceptance/sequence_test.rb