File: spies.feature

package info (click to toggle)
ruby-bogus 0.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 828 kB
  • ctags: 628
  • sloc: ruby: 4,124; makefile: 6; sh: 2
file content (144 lines) | stat: -rw-r--r-- 3,678 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
Feature: Spies

  Object Oriented Programming is all about messages sent between the objects.  If you follow principles like "Tell, Don't Ask", you will enable yourself to combine Bogus's powerful feature of faking objects with it's ability to verify object interactions.

  Typically, stubbing libraries force you to first stub a method, so that you can later make sure it was called. However, if you use fakes, Bogus lets you verify that a method was called (or not) without stubbing it first.

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

    Given a file named "student.rb" with:
    """ruby
    class Student
      def initialize(library)
        @library = library
      end

      def study(*book_titles)
        book_titles.each do |book_title|
          @library.checkout(book_title)
        end
      end
    end
    """

  Scenario: Ensuring methods were called
    Then spec file with following content should pass:
    """ruby
    require_relative 'student'
    require_relative 'library'

    describe Student do
      fake(:library)

      it "studies using books from library" do
        student = Student.new(library)

        student.study("Moby Dick", "Sherlock Holmes")

        expect(library).to have_received.checkout("Moby Dick")
        expect(library).to have_received.checkout("Sherlock Holmes")
      end
    end
    """

  Scenario: Spying on methods that do not exist
    Then spec file with following content should fail:
    """ruby
    require_relative 'student'
    require_relative 'library'

    describe Student do
      fake(:library)

      it "studies using books from library" do
        student = Student.new(library)

        student.study("Moby Dick")

        expect(library).not_to have_received.return_book("Moby Dick")
      end
    end
    """

  Scenario: Spying on methods with wrong number of arguments
    Then spec file with following content should fail:
    """ruby
    require_relative 'student'
    require_relative 'library'

    describe Student do
      fake(:library)

      it "studies using books from library" do
        student = Student.new(library)

        student.study("Moby Dick", "Sherlock Holmes")

        expect(library).not_to have_received.checkout("Moby Dick", 
          "Sherlock Holmes")
      end
    end
    """

  Scenario: Spying on previously stubbed methods
    Then spec file with following content should pass:
    """ruby
    require_relative 'student'
    require_relative 'library'

    describe Student do
      fake(:library)

      it "studies using books from library" do
        stub(library).checkout("Moby Dick") { "checked out" }

        expect(library.checkout("Moby Dick")).to eq("checked out")

        expect(library).to have_received.checkout("Moby Dick")
      end
    end
    """

  Scenario: Spying on attribute writers
    Given a file named "canvas.rb" with:
    """ruby
    class Canvas
      def background_color=(color)
        # do something complicated
      end
    end
    """

    Given a file named "popup.rb" with:
    """ruby
    class Popup
      def self.alert(message, canvas)
        canvas.background_color = "red"
        # display message
      end
    end
    """
   
    Then spec file with following content should pass:
    """ruby
    require_relative 'canvas'
    require_relative 'popup'

    describe Popup do
      fake(:canvas)

      it "sets the background to red" do
        Popup.alert("No such file!", canvas)

        expect(canvas).to have_received(:background_color=, "red")
      end
    end
    """