File: fake_objects.feature

package info (click to toggle)
ruby-bogus 0.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: ruby: 4,237; makefile: 8; sh: 2
file content (146 lines) | stat: -rw-r--r-- 3,749 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Feature: Faking existing classes

  You create a fake by calling the `fake` method:

      fake(fake_name, options) { ClassToCopy }

  If you omit the fake_name, you will get an anonymous fake, otherwise the name will be used to identify the fake for the purposes of contract tests. If you omit the block returning the class to copy, fake name will also be used to guess that.

  Usually you will want the fake to return an instance of the copied class.  Otherwise, you can pass `:as => :class` option to copy the class and return the copy, not an instance of it.

  Options can also include methods to stub on the returned fake, which makes:

      fake(:foo, bar: "value")

  Equivalent to:

      foo = fake(:foo)
      stub(foo).bar(any_args) { "value" }

  For your convenience, Bogus also defines the following macro:

      fake(:foo, bar: "value")

  Which is equivalent to:

      let(:foo) { fake(:foo, bar: "value") }

  Background:
    Given a file named "library.rb" with:
    """ruby
    class Library
      def checkout(book)
      end

      def return_book(book)
      end

      def self.look_up(book)
      end
    end
    """

    Given a file named "student.rb" with:
    """ruby
    class Student
      def self.learn(library = Library.new)
        library.checkout("hello world")
        true
      end
    end
    """

  Scenario: Calling methods that exist on real object
    Then spec file with following content should pass:
    """ruby
    require_relative 'student'
    require_relative 'library'

    describe Student do
      fake(:library)

      it "does something" do
        expect(Student.learn(library)).to be(true)
      end
    end
    """

  Scenario: Taking the guesswork out of finding a class to copy
    Then spec file with following content should pass:
    """ruby
    class PublicLibrary
      def checkout(book)
      end
    end

    describe "logger fake" do
      fake(:library) { PublicLibrary }

      it "uses the class provided in block instead of the guessed one" do
        expect(library.class.name).to eq("PublicLibrary")
      end
    end
    """

  Scenario: Fakes which are classes
    Then spec file with following content should pass:
    """ruby
    require_relative 'library'

    describe "library class fake" do
      fake(:library, as: :class)

      it "is a class" do
        expect(library).to be_a(Class)
      end

      it "has the same name as original class" do
        expect(library.name).to eq(Library.name)
      end

      it "has same methods as original class" do
        library.look_up('something')
      end
    end
    """

  Scenario: Fakes with inline return values
    Then spec file with following content should pass:
    """ruby
    require_relative 'library'

    describe "library class fake" do
      let(:library) { fake(:library, checkout: "checked out", 
                                     return_book: "returned") }

      it "sets the default return value for provided functions" do
        expect(library.checkout("Moby Dick")).to eq("checked out")
        expect(library.checkout("Three Musketeers")).to eq("checked out")
        expect(library.return_book("Moby Dick")).to eq("returned")
        expect(library.return_book("Three Musketeers")).to eq("returned")
      end
    end
    """

  Scenario: Fakes are identified as instances of the faked class
    Then spec file with following content should pass:
    """ruby
    require_relative 'library'

    def library?(object)
      case object
      when Library
        true
      else
        false
      end
    end

    describe "library class fake" do
      fake(:library)

      it "is identified as Library" do
        expect(library?(library)).to be(true)
      end
    end
    """