File: system_example_group_spec.rb

package info (click to toggle)
ruby-rspec-rails 8.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,820 kB
  • sloc: ruby: 10,902; sh: 199; makefile: 6
file content (190 lines) | stat: -rw-r--r-- 5,785 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
module RSpec::Rails
  RSpec.describe SystemExampleGroup do
    it_behaves_like "an rspec-rails example group mixin", :system,
                    './spec/system/', '.\\spec\\system\\'

    describe '#method_name' do
      it 'converts special characters to underscores' do
        group = RSpec::Core::ExampleGroup.describe ActionPack do
          include SystemExampleGroup
        end
        SystemExampleGroup::CHARS_TO_TRANSLATE.each do |char|
          example = group.new
          example_class_mock = double('name' => "method#{char}name")
          allow(example).to receive(:class).and_return(example_class_mock)
          expect(example.send(:method_name)).to start_with('method_name')
        end
      end

      it "handles long method names which include unicode characters" do
        group =
          RSpec::Core::ExampleGroup.describe do
            include SystemExampleGroup
          end

        example = group.new
        allow(example.class).to receive(:name) { "really long unicode example name - #{'あ'*100}" }

        expect(example.send(:method_name).bytesize).to be <= 210
      end
    end

    describe '#driver' do
      it 'uses :selenium_chrome_headless driver by default' do
        group = RSpec::Core::ExampleGroup.describe do
          include SystemExampleGroup
        end
        example = group.new
        group.hooks.run(:before, :example, example)

        expect(Capybara.current_driver).to eq :selenium_chrome_headless
      end

      it 'sets :rack_test driver using by before_action' do
        group = RSpec::Core::ExampleGroup.describe do
          include SystemExampleGroup

          before do
            driven_by(:rack_test)
          end
        end
        example = group.new
        group.hooks.run(:before, :example, example)

        expect(Capybara.current_driver).to eq :rack_test
      end

      it 'calls :driven_by method only once' do
        group = RSpec::Core::ExampleGroup.describe do
          include SystemExampleGroup

          before do
            driven_by(:rack_test)
          end
        end
        example = group.new
        allow(example).to receive(:driven_by).and_call_original
        group.hooks.run(:before, :example, example)

        expect(example).to have_received(:driven_by).once
      end

      it 'calls :served_by method only once' do
        group = RSpec::Core::ExampleGroup.describe do
          include SystemExampleGroup

          before do
            served_by(host: 'rails', port: 8080)
          end
        end
        example = group.new
        allow(example).to receive(:served_by).and_call_original
        group.hooks.run(:before, :example, example)

        expect(example).to have_received(:served_by).once
      end
    end

    describe '#after' do
      xit 'sets the :extra_failure_lines metadata to an array of STDOUT lines' do
        allow(Capybara::Session).to receive(:instance_created?).and_return(true)
        group = RSpec::Core::ExampleGroup.describe do
          include SystemExampleGroup

          before do
            driven_by(:selenium)
          end

          def take_screenshot
            puts 'line 1'
            puts 'line 2'
          end
        end
        example = group.it('fails') { fail }
        group.run

        expect(example.metadata[:extra_failure_lines]).to eq(["line 1\n", "line 2\n"])
      end
    end

    describe '#take_screenshot' do
      xit 'handles Rails calling metadata' do
        allow(Capybara::Session).to receive(:instance_created?).and_return(true)
        group = RSpec::Core::ExampleGroup.describe do
          include SystemExampleGroup

          before do
            driven_by(:selenium)
          end

          def page
            instance_double(Capybara::Session, save_screenshot: nil)
          end
        end
        example = group.it('fails') { raise }
        group.run

        expect(example.metadata[:execution_result].exception).to be_a RuntimeError
      end
    end

    describe '#metadata' do
      let(:group) do
        RSpec::Core::ExampleGroup.describe do
          include SystemExampleGroup
        end
      end

      it 'fakes out the rails expected method' do
        example = group.it('does nothing') {
          metadata[:failure_screenshot_path] = :value
          expect(metadata[:failure_screenshot_path]).to eq(:value)
        }
        group.run
        expect(example.execution_result.status).to eq :passed
      end

      it 'still raises correctly if you use it for something else' do
        examples = []
        examples << group.it('fails nothing') { metadata[:other] = :value }
        examples << group.it('fails nothing') { metadata[:other] }
        examples << group.it('fails nothing') { metadata.key?(:any) }
        group.run
        expect(examples.map(&:execution_result)).to all have_attributes status: :failed
      end
    end

    describe "hook order" do
      it 'calls Capybara.reset_sessions (TestUnit after_teardown) after any after hooks' do
        calls_in_order = []
        allow(Capybara).to receive(:reset_sessions!) { calls_in_order << :reset_sessions! }

        group = RSpec::Core::ExampleGroup.describe do
          include SystemExampleGroup

          before do
            driven_by(:selenium)
          end

          after do
            calls_in_order << :after_hook
          end

          append_after do
            calls_in_order << :append_after_hook
          end

          around do |example|
            example.run
            calls_in_order << :around_hook_after_example
          end
        end
        group.it('works') { }
        group.run

        expect(calls_in_order).to eq([:after_hook, :append_after_hook, :around_hook_after_example, :reset_sessions!])
      end

    end
  end
end