File: README.md

package info (click to toggle)
ruby-minitest-stub-const 0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 100 kB
  • sloc: ruby: 143; makefile: 4
file content (78 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# minitest-stub-const

[![Build Status](https://travis-ci.org/adammck/minitest-stub-const.svg)](https://travis-ci.org/adammck/minitest-stub-const)

Stub constants for the duration of a block in MiniTest.  
Similar to RSpec's [stub_const] [rspec].


## Example
Stub a constant for the duration of a block:

```ruby
module Foo
  BAR = :original
end

Foo.stub_const(:BAR, :stubbed) do
  Foo::BAR
end
# => :stubbed

Foo::BAR
# => :original
```

This is especially useful when testing that the expected class methods
are being called on a `Module` or `Class` instance:

```ruby
module SomeLib
  class Thing
    def self.add
      fail NotImplementedError
    end
  end
end

class ThingAdder
  def add_thing
    SomeLib::Thing.add
  end
end

describe ThingAdder do
  describe '#add_thing' do
    it 'should call Thing.add' do
      adder = ThingAdder.new
      mock = Minitest::Mock.new
      mock.expect(:add, nil)

      SomeLib.stub_const(:Thing, mock) do
        adder.add_thing
      end

      assert mock.verify
    end
  end
end
```

## Installation

```sh
gem install minitest-stub-const # duh
```


## License

[minitest-stub-const] [repo] is free software, available under [the MIT license]
[license].




[repo]: https://raw.github.com/adammck/minitest-stub-const
[license]: https://raw.github.com/adammck/minitest-stub-const/master/LICENSE
[rspec]: https://www.relishapp.com/rspec/rspec-mocks/v/2-12/docs/mutating-constants