flexmock 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,9 +1,25 @@
1
1
  = Changes for FlexMock
2
2
 
3
- == Pre-Version 0.7.1
3
+ == Version 0.8.0
4
+
5
+ * Added by_default
6
+ * Added undefined behavior
7
+ * Mock methods are added to partial mocks only if they are not
8
+ previously defined. This should reduce the need for safe mode.
9
+ * respond_to? on mocks now accepts multiple arguments. This eases
10
+ mocking in rails a little bit.
11
+ * Added experimental view mocking for rails.
12
+
13
+ == Version 0.7.2
14
+
15
+ * Changed initial model Id to 10000.
16
+ * Added test suggested for RSpec Mocks, just to make sure we were good too.
17
+
18
+ == Version 0.7.1
4
19
 
5
20
  * Updated install.rb to handle non-root destination directories via
6
21
  DESTDIR.
22
+ * Fixed operator bug introduced in 0.7.0.
7
23
 
8
24
  == Version 0.7.0
9
25
 
data/README CHANGED
@@ -1,9 +1,9 @@
1
- = Flex Mock -- Making Mock Easy
1
+ examples= Flex Mock -- Making Mock Easy
2
2
 
3
3
  FlexMock is a simple, but flexible, mock object library for Ruby unit
4
4
  testing.
5
5
 
6
- Version :: 0.7.1
6
+ Version :: 0.8.0
7
7
 
8
8
  = Links
9
9
 
@@ -67,7 +67,7 @@ Your test case will look something like this:
67
67
 
68
68
  require 'flexmock/test_unit'
69
69
 
70
- class TestDog < Test::Case::TestCase
70
+ class TestDog < Test::Unit::TestCase
71
71
  def test_dog_wags
72
72
  tail_mock = flexmock(:wag => :happy)
73
73
  assert_equal :happy, tail_mock.wag
@@ -405,6 +405,17 @@ object. See theFlexMock::Expectation for more details.
405
405
  the per-mock ordering are available in the globally ordered method
406
406
  calls.
407
407
 
408
+ * <b>by_default</b>
409
+
410
+ Marks the expectation as a default. Default expectations act as
411
+ normal as long as there are no non-default expectations for the same
412
+ method name. As soon as a non-default expectation is defined, all
413
+ default expectations for that method name are ignored.
414
+
415
+ Default expectations allow you to setup a set of default behaviors
416
+ for various methods in the setup of a test suite, and then override
417
+ only the methods that need special handling in any given test.
418
+
408
419
  * <b>mock</b>
409
420
 
410
421
  Expectation constraints always return the expectation so that the
@@ -486,6 +497,19 @@ The following rules are used for argument matching:
486
497
 
487
498
  will match any even integer.
488
499
 
500
+ * If you wish to match a method call where a block is given, add
501
+ <tt>Proc</tt> as the last argument to <tt>with</tt>.
502
+
503
+ Example:
504
+
505
+ m.should_receive(:foo).with(Integer,Proc).and_return(:got_block)
506
+ m.should_receive(:foo).with(Integer).and_return(:no_block)
507
+
508
+ will cause the mock to return the following:
509
+
510
+ m.foo(1) { } => returns :got_block
511
+ m.foo(1) => returns :no_block
512
+
489
513
  === Creating Partial Mocks
490
514
 
491
515
  Sometimes it is useful to mock the behavior of one or two methods in an
@@ -526,11 +550,11 @@ Here's the test code:
526
550
  # Woofer and return a mock woofer.
527
551
  def setup
528
552
  @dog = Dog.new
529
- flexmock(dog, :bark => :grrr)
553
+ flexmock(@dog, :bark => :grrr)
530
554
  end
531
555
 
532
556
  def test_dog
533
- assert_equal :grrrr, @dog.bark # Mocked Method
557
+ assert_equal :grrr, @dog.bark # Mocked Method
534
558
  assert_equal :happy, @dog.wag # Normal Method
535
559
  end
536
560
  end
@@ -612,6 +636,40 @@ methods at the same time. E.g.
612
636
  m.should_receive(:wag).at_least.once.and_return(:happy)
613
637
  end
614
638
 
639
+ === Default Expectations on Mocks
640
+
641
+ Sometimes you want to setup a bunch of default expectations that are
642
+ pretty much for a number of different tests. Then in the individual
643
+ tests, you would like to override the default behavior on just that
644
+ one method you are testing at the moment. You can do that by using
645
+ the <tt>by_default</tt> modifier.
646
+
647
+ In your test setup you might have:
648
+
649
+ def setup
650
+ @mock_dog = flexmock("Fido")
651
+ @mock_dog.should_receive(:tail => :a_tail, :bark => "woof").by_default
652
+ end
653
+
654
+ The behaviors for :tail and :bark are good for most of the tests, but
655
+ perhaps you wish to verify that :bark is called exactly once in a
656
+ given test. Since :bark by default has no count expectations, you can
657
+ override the default in the given test.
658
+
659
+ def test_something_where_bark_must_be_called_once
660
+ @mock_dog.should_receive(:bark => "woof").once
661
+
662
+ # At this point, the default for :bark is ignored,
663
+ # and the "woof" value will be returned.
664
+
665
+ # However, the default for :tail (which returns :a_tail)
666
+ # is still active.
667
+ end
668
+
669
+ By setting defaults, your individual tests don't have to concern
670
+ themselves with details of all the default setup. But the details of
671
+ the overrides are right there in the body of the test.
672
+
615
673
  === Mocking Law of Demeter Violations
616
674
 
617
675
  The Law of Demeter says that you should only invoke methods on objects
@@ -684,6 +742,19 @@ beforehand.
684
742
  end
685
743
  end
686
744
 
745
+ === Create a mock object that returns an undefined object for method calls
746
+
747
+ require 'flexmock/test_unit'
748
+
749
+ class TestUndefined < Test::Unit::TestCase
750
+ def test_undefined_values
751
+ m = flexmock("mock")
752
+ m.should_receive(:divide_by).with(0).
753
+ and_return_undefined
754
+ assert_equal FlexMock.undefined, m.divide_by(0)
755
+ end
756
+ end
757
+
687
758
  === Expect multiple queries and a single update
688
759
 
689
760
  Multiple calls to the query method will be allows, and calls may have any
@@ -753,8 +824,8 @@ specified indirectly using a symbol.
753
824
  rec.startup.once.ordered
754
825
  rec.query("CPWR") { 12.3 }.once.ordered(:queries)
755
826
  rec.query("MSFT") { 10.0 }.once.ordered(:queries)
756
- rec.query("^....$/) { 3.3 }.at_least.once.ordered(:queries)
757
- rec.finish)once.ordered
827
+ rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
828
+ rec.finish.once.ordered
758
829
  end
759
830
  # test code here using +db+.
760
831
  end
@@ -808,12 +879,15 @@ ignoring.
808
879
  # test code here
809
880
  end
810
881
 
811
- <b>Note:</b> The original +mock_ignore_missing+ is now an alias for
812
- +should_ignore_missing+.
882
+ When +should_ignore_missing+ is enabled, ignored missing methods will
883
+ return an undefined object. Any operation on the undefined object
884
+ will return the undefined object.
813
885
 
814
886
  === Mock just one method on an existing object
815
887
 
816
- The Portfolio class calculate the value of a set of stocks by talking to a quote service via a web service. Since we don't want to use a real web service in our unit tests, we will mock the quote service.
888
+ The Portfolio class calculate the value of a set of stocks by talking
889
+ to a quote service via a web service. Since we don't want to use a
890
+ real web service in our unit tests, we will mock the quote service.
817
891
 
818
892
  def test_portfolio_value
819
893
  flexmock(QuoteService).new_instances do |m|
data/Rakefile CHANGED
@@ -19,7 +19,7 @@ require 'rake/contrib/rubyforgepublisher'
19
19
  CLEAN.include('*.tmp')
20
20
  CLOBBER.include("html", 'pkg')
21
21
 
22
- PKG_VERSION = '0.7.1'
22
+ PKG_VERSION = '0.8.0'
23
23
 
24
24
  PKG_FILES = FileList[
25
25
  '[A-Z]*',
@@ -200,15 +200,15 @@ end
200
200
 
201
201
  # Tagging ------------------------------------------------------------
202
202
 
203
- module Tags
204
- RUBY_FILES = FileList['**/*.rb'].exclude("pkg")
205
- end
203
+ # module Tags
204
+ # RUBY_FILES = FileList['**/*.rb'].exclude("pkg")
205
+ # end
206
206
 
207
- namespace "tags" do
208
- task :emacs => Tags::RUBY_FILES do
209
- puts "Making Emacs TAGS file"
210
- sh "xctags -e #{Tags::RUBY_FILES}", :verbose => false
211
- end
212
- end
207
+ # namespace "tags" do
208
+ # task :emacs => Tags::RUBY_FILES do
209
+ # puts "Making Emacs TAGS file"
210
+ # sh "xctags -e #{Tags::RUBY_FILES}", :verbose => false
211
+ # end
212
+ # end
213
213
 
214
- task :tags => ["tags:emacs"]
214
+ # task :tags => ["tags:emacs"]