micro_mock 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,17 +17,27 @@ gem install micro_mock
17
17
  # create a mock class
18
18
  MyMock = MicroMock.make
19
19
 
20
+ # add a class attr
21
+ MyMock.attr(:foo)
22
+
20
23
  # stub a class method
21
- MyMock.stub(:foo) { "foo" }
24
+ MyMock.stub(:say_foo) { |arg| "#{foo} #{arg}!" }
22
25
 
23
26
  # create a mock instance
24
27
  mock = MyMock.new
25
28
 
26
- # stub an instance method
27
- mock.stub(:bar) { "#{self.class.foo}bar" }
29
+ # add an instance attr
30
+ mock.attr(:bar)
31
+
32
+ # stub some instance methods
33
+ mock.stub(:say_bar) { |arg| "#{bar} #{arg}!" }
34
+
35
+ # use the mock
36
+ MyMock.foo = :foo
37
+ MyMock.say_foo :bar # => "foobar!"
28
38
 
29
- MyMock.foo # => "foo"
30
- mock.bar # => "foobar"
39
+ mock.bar = :bar
40
+ mock.say_bar :foo # => "barfoo!"
31
41
  ```
32
42
 
33
43
  ## Next Steps
@@ -1,3 +1,3 @@
1
1
  module MicroMock
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/micro_mock.rb CHANGED
@@ -1,65 +1,6 @@
1
1
  # MicroMock is a tiny mocking script.
2
- #
3
- # It doesn't make any assumptions about the testing framework.
4
- # It leaves assertions/expectations up to you.
5
- #
6
- # Calling it a mocking script is a bit of a misnomer
7
- # since its really a dynamic class generator.
8
- #
9
- # The term "stub" is used loosely since it adds real behavior...
10
- # and "mocking" a class with real behavior proves to be quite useful.
11
- #
12
- # @example
13
- # # mock a class method
14
- # Mock = MicroMock.make
15
- # Mock.stub(:foo) { true }
16
- #
17
- # # make assertions
18
- # assert Mock.foo # Test::Unit
19
- # Mock.foo.should be_true # RSpec
20
- #
21
- # # mock an instance method
22
- # m = Mock.new
23
- # m.stub(:bar) { false }
24
- #
25
- # # make assertions
26
- # assert_equal false, m.bar # Test::Unit
27
- # m.bar.should eq false # RSpec
28
- #
29
- # # setup mock internal behavior
30
- # count = 1
31
- # m.stub(:a) { count += 1 }
32
- # m.stub(:b) { |i| self.a if i > 5 }
33
- #
34
- # # make assertions
35
- # 10.times { |i| m.b(i) }
36
- # assert_equal 5, count # Test::Unit
37
- # count.should eq 5 # RSpec
38
- #
39
- # Of course you wouldn't normally test the mock itself... rather the code that uses the mock.
40
- # I'll work on adding some real world examples.
41
- #
42
- # @ example
43
- # Mock with a superclass.
44
- # Mock = MicroMock.make(Array)
45
- # list = Mock.new
46
- # list << 1
47
- # list.stub :say_hi do |name|
48
- # "Hi #{name}!"
49
- # end
50
- # list.say_hi "Nate" # => "Hi Nate!"
51
2
  module MicroMock
52
3
 
53
- # Stubs a method.
54
- # The term stub is used loosely since it adds real functionality.
55
- # @param [Symbol] name The name of the method.
56
- # @yield The block that will serve as the method definition.
57
- def stub(name, &block)
58
- context = class << self; self; end if is_a? Class
59
- context ||= self.class
60
- context.send :define_method, name, &block
61
- end
62
-
63
4
  # Defines a mock class.
64
5
  def self.make(superclass=Object)
65
6
  klass = Class.new(superclass) do
@@ -74,4 +15,25 @@ module MicroMock
74
15
  klass
75
16
  end
76
17
 
18
+ # Creates an attribute getter & setter.
19
+ # @param [Symbol] name The name of the attribute.
20
+ def attr(name)
21
+ context.send :attr_accessor, name
22
+ end
23
+
24
+ # Stubs a method.
25
+ # The term stub is used loosely since it adds real functionality.
26
+ # @param [Symbol] name The name of the method.
27
+ # @yield The block that will serve as the method definition.
28
+ def stub(name, &block)
29
+ context.send :define_method, name, &block
30
+ end
31
+
32
+ private
33
+
34
+ def context
35
+ @context = class << self; self; end if is_a? Class
36
+ @context ||= self.class
37
+ end
38
+
77
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micro_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-19 00:00:00.000000000 Z
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! ' Perhaps the lightest mocking strategy available.
15
15