File: independent_matchers_spec.rb

package info (click to toggle)
ruby-shoulda-matchers 7.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 34,046; sh: 280; makefile: 9
file content (125 lines) | stat: -rw-r--r-- 3,007 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
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
require 'acceptance_spec_helper'

describe 'shoulda-matchers has independent matchers, specifically delegate_method' do
  specify 'and integrates with a Ruby application that uses the default test framework' do
    create_generic_bundler_project

    updating_bundle do
      add_minitest_to_project
      add_shoulda_context_to_project(manually: true)
      add_shoulda_matchers_to_project(
        test_frameworks: [default_test_framework],
        manually: true,
      )
    end

    write_file 'lib/post_office.rb', <<-FILE
      class PostOffice
      end
    FILE

    write_file 'lib/courier.rb', <<-FILE
      require 'forwardable'

      class Courier
        extend Forwardable

        def_delegators :post_office, :deliver

        attr_reader :post_office

        def initialize(post_office)
          @post_office = post_office
        end
      end
    FILE

    write_n_unit_test 'test/courier_test.rb' do |test_case_superclass|
      <<-FILE
        require 'test_helper'
        require 'courier'
        require 'post_office'

        class CourierTest < #{test_case_superclass}
          subject { Courier.new(post_office) }

          should delegate_method(:deliver).to(:post_office)

          def post_office
            PostOffice.new
          end
        end
      FILE
    end

    result = run_n_unit_tests('test/courier_test.rb - -v')

    expect(result).to indicate_number_of_tests_was_run(1)
    expect(result).to have_output(
      'Courier should delegate #deliver to the #post_office object',
    )
  end

  specify 'and integrates with a Ruby application that uses RSpec' do
    create_generic_bundler_project

    updating_bundle do
      add_rspec_to_project
      add_shoulda_matchers_to_project(
        manually: true,
        with_configuration: false,
      )
      write_file 'spec/spec_helper.rb', <<-FILE
        require 'shoulda/matchers/independent'

        RSpec.configure do |config|
          config.include(Shoulda::Matchers::Independent)
        end
      FILE
    end

    write_file 'lib/post_office.rb', <<-FILE
      class PostOffice
      end
    FILE

    write_file 'lib/courier.rb', <<-FILE
      require 'forwardable'

      class Courier
        extend Forwardable

        def_delegators :post_office, :deliver

        attr_reader :post_office

        def initialize(post_office)
          @post_office = post_office
        end
      end
    FILE

    write_file 'spec/courier_spec.rb', <<-FILE
      require 'spec_helper'
      require 'courier'
      require 'post_office'

      describe Courier do
        subject { Courier.new(post_office) }

        it { should delegate_method(:deliver).to(:post_office) }

        def post_office
          PostOffice.new
        end
      end
    FILE

    result = run_rspec_tests('spec/courier_spec.rb')

    expect(result).to indicate_number_of_tests_was_run(1)
    expect(result).to have_output(
      /Courier\s+is expected to delegate #deliver to the #post_office object/,
    )
  end
end