Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial import

  • Loading branch information...
commit 4a5818e0402245c4345e5b3ab6a3f3b8b802a1d7 0 parents
@cfcosta authored
8 Rakefile
@@ -0,0 +1,8 @@
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.libs << ['lib', 'test']
+ t.test_files = FileList['test/*/*_test.rb']
+ t.verbose = true
+ t.warning = true
+end
33 lib/minitest/fire_mock.rb
@@ -0,0 +1,33 @@
+require 'minitest/mock'
+
+class MiniTest::FireMock < MiniTest::Mock
+ def initialize(constant)
+ @constant_name = constant
+ @constant = constantize(constant)
+
+ super()
+ end
+
+ def expect(name, retval, args = [])
+ if @constant and not @constant.instance_methods.include? name
+ raise MockExpectationError, "expected #{@constant_name} to define `#{name}`, but it doesn't"
+ end
+
+ super(name, retval, args)
+ end
+
+ private
+ # Borrowed from ActiveSupport.
+ def constantize(camel_cased_word)
+ names = camel_cased_word.split('::')
+ names.shift if names.empty? || names.first.empty?
+
+ constant = Object
+ names.each do |name|
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
+ end
+ constant
+ rescue NameError
+ nil
+ end
+end
14 test/test_helper.rb
@@ -0,0 +1,14 @@
+require 'minitest/unit'
+require 'minitest/fire_mock'
+
+MiniTest::Unit.autorun
+
+class DefinedConstant
+ def defined_method; end
+end
+
+module Namespace
+ class NamespacedConstant
+ def defined_method; end
+ end
+end
46 test/unit/firemock_test.rb
@@ -0,0 +1,46 @@
+require 'test_helper'
+require 'minitest/fire_mock'
+
+class FireMockTest < MiniTest::Unit::TestCase
+ def test_mock_is_valid_when_not_defined
+ mock = MiniTest::FireMock.new('NotDefinedConstant')
+ mock.expect(:defined_method, 42)
+ assert_equal 42, mock.defined_method
+ mock.verify
+ end
+
+ def test_mock_is_valid_when_defined_and_responds_to_method
+ mock = MiniTest::FireMock.new('DefinedConstant')
+ mock.expect(:defined_method, 42)
+ assert_equal 42, mock.defined_method
+ mock.verify
+ end
+
+ def test_mock_is_invalid_when_defined_but_dont_responds_to_method
+ mock = MiniTest::FireMock.new('DefinedConstant')
+ assert_raises MockExpectationError, "expected Foo to define `not_defined_method`, but it doesn't" do
+ mock.expect(:not_defined_method, 42)
+ end
+ end
+
+ def test_mock_with_namespace_is_valid_when_not_defined
+ mock = MiniTest::FireMock.new('Namespace::NotDefinedConstant')
+ mock.expect(:defined_method, 42)
+ assert_equal 42, mock.defined_method
+ mock.verify
+ end
+
+ def test_mock_with_namespace_is_valid_when_defined_and_responds_to_method
+ mock = MiniTest::FireMock.new('Namespace::NamespacedConstant')
+ mock.expect(:defined_method, 42)
+ assert_equal 42, mock.defined_method
+ mock.verify
+ end
+
+ def test_mock_with_namespace_is_invalid_when_defined_but_dont_responds_to_method
+ mock = MiniTest::FireMock.new('Namespace::NamespacedConstant')
+ assert_raises MockExpectationError, "expected Foo to define `not_defined_method`, but it doesn't" do
+ mock.expect(:not_defined_method, 42)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.