Permalink
Browse files

Tagging 0.4.0 release.

  • Loading branch information...
floehopper committed Jun 24, 2008
1 parent 784295f commit 04b02f0f900f6882bb4e856d762e6675d2d323a9
Showing with 3,911 additions and 0 deletions.
  1. +3 −0 trunk/COPYING
  2. +7 −0 trunk/MIT-LICENSE
  3. +35 −0 trunk/README
  4. +66 −0 trunk/RELEASE
  5. +126 −0 trunk/Rakefile
  6. +49 −0 trunk/TODO
  7. +36 −0 trunk/examples/misc.rb
  8. +26 −0 trunk/examples/mocha.rb
  9. +65 −0 trunk/examples/stubba.rb
  10. +4 −0 trunk/init.rb
  11. +19 −0 trunk/lib/mocha.rb
  12. +35 −0 trunk/lib/mocha/any_instance_method.rb
  13. +113 −0 trunk/lib/mocha/auto_verify.rb
  14. +35 −0 trunk/lib/mocha/central.rb
  15. +62 −0 trunk/lib/mocha/class_method.rb
  16. +295 −0 trunk/lib/mocha/expectation.rb
  17. +6 −0 trunk/lib/mocha/expectation_error.rb
  18. +27 −0 trunk/lib/mocha/infinite_range.rb
  19. +37 −0 trunk/lib/mocha/inspect.rb
  20. +8 −0 trunk/lib/mocha/instance_method.rb
  21. +7 −0 trunk/lib/mocha/metaclass.rb
  22. +20 −0 trunk/lib/mocha/mock.rb
  23. +122 −0 trunk/lib/mocha/mock_methods.rb
  24. +100 −0 trunk/lib/mocha/object.rb
  25. +28 −0 trunk/lib/mocha/pretty_parameters.rb
  26. +23 −0 trunk/lib/mocha/setup_and_teardown.rb
  27. +30 −0 trunk/lib/mocha/standalone.rb
  28. +49 −0 trunk/lib/mocha/test_case_adapter.rb
  29. +2 −0 trunk/lib/mocha_standalone.rb
  30. +2 −0 trunk/lib/stubba.rb
  31. +36 −0 trunk/test/active_record_test_case.rb
  32. +75 −0 trunk/test/all_tests.rb
  33. +34 −0 trunk/test/execution_point.rb
  34. +18 −0 trunk/test/method_definer.rb
  35. +124 −0 trunk/test/mocha/any_instance_method_test.rb
  36. +163 −0 trunk/test/mocha/auto_verify_test.rb
  37. +124 −0 trunk/test/mocha/central_test.rb
  38. +196 −0 trunk/test/mocha/class_method_test.rb
  39. +357 −0 trunk/test/mocha/expectation_test.rb
  40. +50 −0 trunk/test/mocha/infinite_range_test.rb
  41. +90 −0 trunk/test/mocha/inspect_test.rb
  42. +22 −0 trunk/test/mocha/metaclass_test.rb
  43. +235 −0 trunk/test/mocha/mock_methods_test.rb
  44. +84 −0 trunk/test/mocha/mock_test.rb
  45. +165 −0 trunk/test/mocha/object_test.rb
  46. +32 −0 trunk/test/mocha/pretty_parameters_test.rb
  47. +76 −0 trunk/test/mocha/setup_and_teardown_test.rb
  48. +98 −0 trunk/test/mocha_acceptance_test.rb
  49. +105 −0 trunk/test/mocha_test_result_integration_test.rb
  50. +110 −0 trunk/test/standalone_acceptance_test.rb
  51. +102 −0 trunk/test/stubba_acceptance_test.rb
  52. +89 −0 trunk/test/stubba_integration_test.rb
  53. +85 −0 trunk/test/stubba_test_result_integration_test.rb
  54. +4 −0 trunk/test/test_helper.rb
View
@@ -0,0 +1,3 @@
+Copyright Revieworld Ltd. 2006
+
+You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt) or under the MIT license (see MIT-LICENSE file).
View
@@ -0,0 +1,7 @@
+Copyright (c) 2006 Revieworld Ltd.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,35 @@
+= Mocha
+
+Mocha is a library for mocking and stubbing using a syntax like that of JMock[http://www.jmock.org], and SchMock[http://rubyforge.org/projects/schmock]. Most commonly Mocha is used in conjunction with Test::Unit[http://www.ruby-doc.org/core/classes/Test/Unit.html], but it can be used in other contexts.
+
+One of its main advantages is that it allows you to mock and stub methods on _real_ (non-mock) classes and instances. You can for example stub ActiveRecord[http://api.rubyonrails.com/classes/ActiveRecord/Base.html] instance methods like +create+, +save+, +destroy+ and even class methods like +find+ to avoid hitting the database in unit tests.
+
+Mocha provides a unified, simple and readable syntax for both traditional mocking and for mocking with _real_ objects.
+
+Mocha has been harvested from projects at Reevoo[http://www.reevoo.com] by me (James[http://blog.floehopper.org]) and my colleagues Ben[http://www.reevoo.com/blogs/bengriffiths], Chris[http://blog.seagul.co.uk] and Paul[http://po-ru.com]. Mocha is in use on real-world Rails[http://www.rubyonrails.org] projects.
+
+== Download and Installation
+
+Install the gem with the following command...
+
+ $ gem install mocha
+
+Or install the Rails[http://www.rubyonrails.org] plugin...
+
+ $ script/plugin install svn://rubyforge.org/var/svn/mocha/trunk
+
+Or download Mocha from here - http://rubyforge.org/projects/mocha
+
+== Examples
+
+* Quick Start - {Usage Examples}[link:examples/misc.html]
+* Traditional mocking - {Star Trek Example}[link:examples/mocha.html]
+* Setting expectations on real classes - {Order Example}[link:examples/stubba.html]
+* More examples on {Floehopper's Blog}[http://blog.floehopper.org]
+* {Mailing List Archives}[http://rubyforge.org/pipermail/mocha-developer/]
+
+== License
+
+Copyright Revieworld Ltd. 2006
+
+You may use, copy and redistribute this library under the same terms as {Ruby itself}[http://www.ruby-lang.org/en/LICENSE.txt] or under the {MIT license}[http://mocha.rubyforge.org/files/MIT-LICENSE.html].
View
@@ -0,0 +1,66 @@
+= 0.3.0
+
+* Rails plugin.
+* Auto-verify for expectations on concrete classes.
+* Include each expectation verification in the test result assertion count.
+* Filter out noise from assertion backtraces.
+* Point assertion backtrace to line where failing expectation was created.
+* New yields method for expectations.
+* Create stubs which stub all method calls.
+* Mocks now respond_to? expected methods.
+
+= 0.2.1
+
+* Rename MochaAcceptanceTest::Rover#move method to avoid conflict with Rake (in Ruby 1.8.4 only?)
+
+= 0.2.0
+
+* Small change to SetupAndTeardown#teardown_stubs suggested by Luke Redpath (http://www.lukeredpath.co.uk) to allow use of Stubba with RSpec (http://rspec.rubyforge.org).
+* Reorganized directory structure and extracted addition of setup and teardown methods into SmartTestCase mini-library.
+* Addition of auto-verify for Mocha (but not Stubba). This means there is more significance in the choice of expects or stubs in that any expects on a mock will automatically get verified.
+
+So instead of...
+
+ wotsit = Mocha.new
+ wotsit.expects(:thingummy).with(5).returns(10)
+ doobrey = Doobrey.new(wotsit)
+ doobrey.hoojamaflip
+ wotsit.verify
+
+you need to do...
+
+ wotsit = mock()
+ wotsit.expects(:thingummy).with(5).returns(10)
+ doobrey = Doobrey.new(wotsit)
+ doobrey.hoojamaflip
+ # no need to verify
+
+There are also shortcuts as follows...
+
+instead of...
+
+ wotsit = Mocha.new
+ wotsit.expects(:thingummy).returns(10)
+ wotsit.expects(:summat).returns(25)
+
+you can have...
+
+ wotsit = mock(:thingummy => 5, :summat => 25)
+
+and instead of...
+
+ wotsit = Mocha.new
+ wotsit.stubs(:thingummy).returns(10)
+ wotsit.stubs(:summat).returns(25)
+
+you can have...
+
+ wotsit = stub(:thingummy => 5, :summat => 25)
+
+= 0.1.2
+
+* Minor tweaks
+
+= 0.1.1
+
+* Initial release.
View
@@ -0,0 +1,126 @@
+require 'rubygems'
+require 'rake/rdoctask'
+require 'rake/gempackagetask'
+require 'rake/contrib/sshpublisher'
+
+module Mocha
+ VERSION = "0.4.0"
+end
+
+desc "Default task is currently to run all tests"
+task :default => :test_all
+
+desc "Run all tests"
+task :test_all do
+ $: << "#{File.dirname(__FILE__)}/test"
+ require 'test/all_tests'
+end
+
+desc 'Generate RDoc'
+Rake::RDocTask.new do |task|
+ task.main = 'README'
+ task.title = 'Mocha'
+ task.rdoc_dir = 'doc'
+ task.template = "html_with_google_analytics"
+ task.options << "--line-numbers" << "--inline-source"
+ task.rdoc_files.include('README', 'RELEASE', 'COPYING', 'MIT-LICENSE', 'agiledox.txt', 'lib/mocha/auto_verify.rb', 'lib/mocha/mock_methods.rb', 'lib/mocha/expectation.rb', 'lib/mocha/object.rb')
+end
+task :rdoc => :examples
+
+desc "Upload RDoc to RubyForge"
+task :publish_rdoc => [:rdoc, :examples] do
+ Rake::SshDirPublisher.new("jamesmead@rubyforge.org", "/var/www/gforge-projects/mocha", "doc").upload
+end
+
+desc "Generate agiledox-like documentation for tests"
+file 'agiledox.txt' do
+ File.open('agiledox.txt', 'w') do |output|
+ tests = FileList['test/**/*_test.rb']
+ tests.each do |file|
+ m = %r".*/([^/].*)_test.rb".match(file)
+ output << m[1]+" should:\n"
+ test_definitions = File::readlines(file).select {|line| line =~ /.*def test.*/}
+ test_definitions.sort.each do |definition|
+ m = %r"test_(should_)?(.*)".match(definition)
+ output << " - "+m[2].gsub(/_/," ") << "\n"
+ end
+ end
+ end
+end
+
+desc "Convert example ruby files to syntax-highlighted html"
+task :examples do
+ require 'coderay'
+ mkdir_p 'doc/examples'
+ File.open('doc/examples/coderay.css', 'w') do |output|
+ output << CodeRay::Encoders[:html]::CSS.new.stylesheet
+ end
+ ['mocha', 'stubba', 'misc'].each do |filename|
+ File.open("doc/examples/#{filename}.html", 'w') do |file|
+ file << "<html>"
+ file << "<head>"
+ file << %q(<link rel="stylesheet" media="screen" href="coderay.css" type="text/css">)
+ file << "</head>"
+ file << "<body>"
+ file << CodeRay.scan_file("examples/#{filename}.rb").html.div
+ file << "</body>"
+ file << "</html>"
+ end
+ end
+end
+
+Gem::manage_gems
+
+specification = Gem::Specification.new do |s|
+ s.name = "mocha"
+ s.summary = "Mocking and stubbing library"
+ s.version = Mocha::VERSION
+ s.author = 'James Mead'
+ s.description = <<-EOF
+ Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
+ EOF
+ s.email = 'mocha-developer@rubyforge.org'
+ s.homepage = 'http://mocha.rubyforge.org'
+ s.rubyforge_project = 'mocha'
+
+ s.has_rdoc = true
+ s.extra_rdoc_files = ['README', 'COPYING']
+ s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README' << '--line-numbers'
+
+ s.autorequire = 'mocha'
+ s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*'].exclude('TODO').to_a
+ s.test_file = "test/all_tests.rb"
+end
+
+Rake::GemPackageTask.new(specification) do |package|
+ package.need_zip = true
+ package.need_tar = true
+end
+
+task :verify_user do
+ raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
+end
+
+task :verify_password do
+ raise "RUBYFORGE_PASSWORD environment variable not set!" unless ENV['RUBYFORGE_PASSWORD']
+end
+
+desc "Publish package files on RubyForge."
+task :publish_packages => [:verify_user, :verify_password, :package] do
+ require 'meta_project'
+ require 'rake/contrib/xforge'
+ release_files = FileList[
+ "pkg/mocha-#{Mocha::VERSION}.gem",
+ "pkg/mocha-#{Mocha::VERSION}.tgz",
+ "pkg/mocha-#{Mocha::VERSION}.zip"
+ ]
+
+ Rake::XForge::Release.new(MetaProject::Project::XForge::RubyForge.new('mocha')) do |release|
+ release.user_name = ENV['RUBYFORGE_USER']
+ release.password = ENV['RUBYFORGE_PASSWORD']
+ release.files = release_files.to_a
+ release.release_name = "Mocha #{Mocha::VERSION}"
+ release.release_changes = ''
+ release.release_notes = ''
+ end
+end
View
@@ -0,0 +1,49 @@
+=> restrict partial mocks to existing public methods?
+- allow specification of class to be mocked, so we can check for existence of methods
+- investigate where/why check for existence of stubbed method has gone.
+- allow stubbing of private/protected methods? - force_stub?
+
+=> multiple expectations for same method
+- look at JMock for examples of when multiple expectations match
+- increment actual count for ANY expectation that matches - see email from Bryan Helmkamp
+- think about allowing a stubbing expectation to be converted to an asserting expectation and vice versa
+- fail fast if expectation count exceeded during test...? c.f. JMock?
+- think about behaviour when more than one expectation/stubbed method match c.f. JMock?
+
+=> reduce footprint of mocha in terms of visible methods
+- do away with __is_a__ method if possible - Proc param may no longer be needed
+- add similar test to test_should_be_able_to_mock_standard_object_methods for partial mocks - rename non-public method with underscores e.g. mocha, reset_mocha, stubba_method, stubba_object, etc.
+- reduce number of methods added to Object, Class etc to bare minimum
+- reduce number of methods excluded from undef in mock_methods (maybe use blank_slate as mocha parent class to allow mocking of standard object methods?)
+- perhaps only add methods to particular class at point where expects or stubs gets called
+- provide some means to un-stubba an object - ideally should restore any methods with same names as stubba methods c.f. dealing with expectations for methods called :expects, :stubs, etc.
+- use Object#inspect(:mocha) or Object#__inspect__ instead of Object#mocha_inspect?
+
+=> improve design
+- use builder object c.f. JMock
+- possibly simplify by making mock the same as a partial mock of Object
+- provide test::unit agnostic api - mockery/context type objects on which the user must call setup/teardown?
+
+=> release management
+- why does gem rdoc include all source files?
+- tags for repos
+- automate releases cf rspec
+
+=> documentation
+- read and improve tutorial emailed by user
+
+=> naming
+- default mock names to mock1, mock2, etc or something similar
+- stubba mocks should named according to parent stubbee
+
+=> improve tests
+- use real activerecord as svn external to for testing instead of active_record_test_case
+- test for setting expectations on class methods (and instance methods?) from within TestCase#setup
+
+=> possible extra functionality
+- add multiyield - see email from Jay Fields
+- allow hash parameter for stubs and expects methods particularly for stubba
+- more jmock style stuff - e.g. labels/required order, more sophisticated param matching?
+- maybe allow unstubbing of a specific method from within a test...?
+- oomatron type ideas
+- should all instances share expectations for any_instance or should each instance have their own - in which case how do we provide access to the instances
View
@@ -0,0 +1,36 @@
+# Mocking a class method
+
+ product = Product.new
+ Product.expects(:find).with(1).returns(product)
+ assert_equal product, Product.find(1)
+
+# Mocking an instance method on a real object
+
+ product = Product.new
+ product.expects(:save).returns(true)
+ assert product.save
+
+# Stubbing instance methods on real object
+
+ prices = [stub(:pence => 1000), stub(:pence => 2000)]
+ product = Product.new
+ product.stubs(:prices).returns(prices)
+ assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
+
+# Stubbing an instance method on all instances of a class
+
+ Product.any_instance.stubs(:name).returns('stubbed_name')
+ product = Product.new
+ assert_equal 'stubbed_name', product.name
+
+# Traditional mocking
+
+ object = mock()
+ object.expects(:expected_method).with(:p1, :p2).returns(:result)
+ assert_equal :result, object.expected_method(:p1, :p2)
+
+# Shortcuts
+
+ object = stub(:method1 => :result1, :method2 => :result2)
+ assert_equal :result1, object.method1
+ assert_equal :result2, object.method2
View
@@ -0,0 +1,26 @@
+class Enterprise
+
+ def initialize(dilithium)
+ @dilithium = dilithium
+ end
+
+ def go(warp_factor)
+ warp_factor.times { @dilithium.nuke(:anti_matter) }
+ end
+
+end
+
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+
+class EnterpriseTest < Test::Unit::TestCase
+
+ def test_should_boldly_go
+ dilithium = mock()
+ dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
+ enterprise = Enterprise.new(dilithium)
+ enterprise.go(2)
+ end
+
+end
Oops, something went wrong.

0 comments on commit 04b02f0

Please sign in to comment.