File: contract_tests_stubs.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 (126 lines) | stat: -rw-r--r-- 3,047 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
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
Feature: Contract tests with stubs

  Whenever you stub any method, a contract is specified on the input/output values of that method.

  When stubbing using the short syntax:

      fake(:fake_name, method_name: :return_value)

  the contract can only be specified on the return value.

  The longer syntax:

      stub(fake).method_name(args) { :return_value }

  will also create a contract on the method input parameters.

  Background:
    Given a file named "library.rb" with:
    """ruby
      class Library
        def initialize
          @books = []
        end

        def has_book?(book)
          @books.include?(book)
        end

        def checkout(book)
          @books.delete(book)
        end

        def return(book)
          @books << book
        end
      end
    """

    Given a file named "student.rb" with:
    """ruby
      class Student
        def read(book, library = Library.new)
          if library.has_book?(book)
            library.checkout(book)
          end
        end
      end
    """

    And a spec file named "student_spec.rb" with:
    """ruby
    require_relative 'student'
    require_relative 'library'

    describe Student do
      fake(:library)

      it "checks out the book from library if it is available" do
        student = Student.new
        stub(library).has_book?("Moby Dick") { true }

        student.read("Moby Dick", library)

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

      it "does not check out the book from library if not available" do
        student = Student.new
        stub(library).has_book?("Moby Dick") { false }

        student.read("Moby Dick", library)

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

  Scenario: Fails when stubbed methods are not called on real object
    Then spec file with following content should fail:
    """ruby
    require_relative 'library'

    describe Library do
      verify_contract(:library)

      let(:library) { Library.new }

      it "marks books as unavailable after they are checked out" do
        library.return("Moby Dick")

        library.checkout("Moby Dick")

        expect(library.has_book?("Moby Dick")).to be_false
      end
    end
    """

  Scenario: Verifies that stubbed methods are called
    Then spec file with following content should pass:
    """ruby
    require_relative 'library'

    describe Library do
      verify_contract(:library)

      let(:library) { described_class.new }

      it "allows checking out books that are in the inventory" do
        library.return("Moby Dick")

        expect(library.has_book?("Moby Dick")).to be_true
      end

      it "does not allow checking out unavailable books" do
        expect(library.has_book?("Moby Dick")).to be_false
      end

      it "marks books as unavailable after they are checked out" do
        library.return("Moby Dick")

        library.checkout("Moby Dick")

        expect(library.has_book?("Moby Dick")).to be_false
      end
    end
    """