minitest-firemock 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ MiniTest::FireMock
2
+ ==================
3
+
4
+ This gem was designed do make isolated tests more resilient. In isolated tests, a FireMock is no different than a common mock. The only difference is when the test is called on a not-isolated environment. It checks for the presence of the method on the mocked class, and fails if it isn't there. This adds another layer of security for suit tests, without compromising the isolation of unit tests.
5
+
6
+ It's based on the awesome [rspec-fire](https://github.com/xaviershay/rspec-fire) from [Xavier Shay](http://xaviershay.com/).
7
+
8
+ Usage
9
+ -----
10
+
11
+ ```ruby
12
+ require 'minitest/autorun'
13
+ require 'minitest/fire_mock'
14
+
15
+ class MyClass
16
+ def my_method
17
+ # actual_work goes here
18
+ end
19
+ end
20
+
21
+ class MyOtherClassTest < MiniTest::Unit::TestCase
22
+ def test_for_correctness
23
+ mock = MiniTest::FireMock('MyClass')
24
+ mock.expect(:my_method, 42)
25
+ assert_equal 42, mock.my_method
26
+ mock.verify
27
+ end
28
+ end
29
+ ```
30
+
31
+ The only real difference of using `MiniTest::FireMock` instead of `MiniTest::Mock` is that if `MyClass` is defined, and `my_method` isn't there, it'll raise a `MockExpectationError`.
32
+
33
+ TODO
34
+ ----
35
+
36
+ - Check for the arity of the methods if they are defined.
37
+ - Mock class/module methods too.
38
+ - Make it work with method_missing (as of now it doesn't, even if the #responds_to? is correct)
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'bundler/gem_tasks'
3
+ rescue LoadError
4
+ warn "No bundler found, you won't be able to build the gem."
5
+ end
6
+
7
+ require 'rake/testtask'
8
+
9
+ Rake::TestTask.new do |t|
10
+ t.libs << ['lib', 'test']
11
+ t.test_files = FileList['test/*/*_test.rb']
12
+ t.verbose = true
13
+ t.warning = true
14
+ end
@@ -0,0 +1,33 @@
1
+ require 'minitest/mock'
2
+
3
+ class MiniTest::FireMock < MiniTest::Mock
4
+ def initialize(constant)
5
+ @constant_name = constant
6
+ @constant = constantize(constant)
7
+
8
+ super()
9
+ end
10
+
11
+ def expect(name, retval, args = [])
12
+ if @constant and not @constant.instance_methods.include? name
13
+ raise MockExpectationError, "expected #{@constant_name} to define `#{name}`, but it doesn't"
14
+ end
15
+
16
+ super(name, retval, args)
17
+ end
18
+
19
+ private
20
+ # Borrowed from ActiveSupport.
21
+ def constantize(camel_cased_word)
22
+ names = camel_cased_word.split('::')
23
+ names.shift if names.empty? || names.first.empty?
24
+
25
+ constant = Object
26
+ names.each do |name|
27
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
28
+ end
29
+ constant
30
+ rescue NameError
31
+ nil
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |gem|
3
+ gem.version = '0.0.1'
4
+
5
+ gem.authors = ["Cainã Costa"]
6
+ gem.email = ["cainan.costa@gmail.com"]
7
+ gem.description = %q{Makes your MiniTest mocks more resilient.}
8
+ gem.summary = %q{Makes your MiniTest mocks more resilient.}
9
+ gem.homepage = "https://github.com/sryche/minitest-firemock"
10
+
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- test/*`.split("\n")
13
+ gem.name = "minitest-firemock"
14
+ gem.require_paths = ["lib"]
15
+ end
@@ -0,0 +1,14 @@
1
+ require 'minitest/unit'
2
+ require 'minitest/fire_mock'
3
+
4
+ MiniTest::Unit.autorun
5
+
6
+ class DefinedConstant
7
+ def defined_method; end
8
+ end
9
+
10
+ module Namespace
11
+ class NamespacedConstant
12
+ def defined_method; end
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+ require 'minitest/fire_mock'
3
+
4
+ class FireMockTest < MiniTest::Unit::TestCase
5
+ def test_mock_is_valid_when_not_defined
6
+ mock = MiniTest::FireMock.new('NotDefinedConstant')
7
+ mock.expect(:defined_method, 42)
8
+ assert_equal 42, mock.defined_method
9
+ mock.verify
10
+ end
11
+
12
+ def test_mock_is_valid_when_defined_and_responds_to_method
13
+ mock = MiniTest::FireMock.new('DefinedConstant')
14
+ mock.expect(:defined_method, 42)
15
+ assert_equal 42, mock.defined_method
16
+ mock.verify
17
+ end
18
+
19
+ def test_mock_is_invalid_when_defined_but_dont_responds_to_method
20
+ mock = MiniTest::FireMock.new('DefinedConstant')
21
+ assert_raises MockExpectationError, "expected Foo to define `not_defined_method`, but it doesn't" do
22
+ mock.expect(:not_defined_method, 42)
23
+ end
24
+ end
25
+
26
+ def test_mock_with_namespace_is_valid_when_not_defined
27
+ mock = MiniTest::FireMock.new('Namespace::NotDefinedConstant')
28
+ mock.expect(:defined_method, 42)
29
+ assert_equal 42, mock.defined_method
30
+ mock.verify
31
+ end
32
+
33
+ def test_mock_with_namespace_is_valid_when_defined_and_responds_to_method
34
+ mock = MiniTest::FireMock.new('Namespace::NamespacedConstant')
35
+ mock.expect(:defined_method, 42)
36
+ assert_equal 42, mock.defined_method
37
+ mock.verify
38
+ end
39
+
40
+ def test_mock_with_namespace_is_invalid_when_defined_but_dont_responds_to_method
41
+ mock = MiniTest::FireMock.new('Namespace::NamespacedConstant')
42
+ assert_raises MockExpectationError, "expected Foo to define `not_defined_method`, but it doesn't" do
43
+ mock.expect(:not_defined_method, 42)
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-firemock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cainã Costa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-08 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Makes your MiniTest mocks more resilient.
15
+ email:
16
+ - cainan.costa@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - Rakefile
23
+ - lib/minitest/fire_mock.rb
24
+ - minitest-firemock.gemspec
25
+ - test/test_helper.rb
26
+ - test/unit/firemock_test.rb
27
+ homepage: https://github.com/sryche/minitest-firemock
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.10
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Makes your MiniTest mocks more resilient.
51
+ test_files:
52
+ - test/test_helper.rb
53
+ - test/unit/firemock_test.rb