File: stub_undefined_constant.feature

package info (click to toggle)
ruby-rspec-mocks 2.14.3-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 868 kB
  • sloc: ruby: 8,131; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,602 bytes parent folder | download | duplicates (2)
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
Feature: Stub Undefined Constant

  Use `stub_const` to stub constants. When the constant is not already defined,
  all the necessary intermediary modules will be dynamically created. When the
  example completes, the intermediary module constants will be removed to return
  the constant state to how it started.

  Scenario: Stub top-level constant
    Given a file named "stub_const_spec.rb" with:
      """ruby
      describe "stubbing FOO" do
        it "can stub undefined constant FOO" do
          stub_const("FOO", 5)
          FOO.should eq(5)
        end

        it "undefines the constant when the example completes" do
          expect { FOO }.to raise_error(NameError)
        end
      end
      """
    When I run `rspec stub_const_spec.rb`
    Then the examples should all pass

  Scenario: Stub nested constant
    Given a file named "stub_const_spec.rb" with:
      """ruby
      module MyGem
        class SomeClass
        end
      end

      module MyGem
        describe SomeClass do
          it "can stub an arbitrarily deep constant that is undefined" do
            defined?(SomeClass::A).should be_false
            stub_const("MyGem::SomeClass::A::B::C", 3)
            SomeClass::A::B::C.should eq(3)
            SomeClass::A.should be_a(Module)
          end

          it 'undefines the intermediary constants that were dynamically created' do
            defined?(SomeClass).should be_true
            defined?(SomeClass::A).should be_false
          end
        end
      end
      """
    When I run `rspec stub_const_spec.rb`
    Then the examples should all pass