File: test_double_spec.rb

package info (click to toggle)
ruby-rspec 3.9.0c2e2m1s3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,612 kB
  • sloc: ruby: 67,456; sh: 1,572; makefile: 98
file content (58 lines) | stat: -rw-r--r-- 1,666 bytes parent folder | download | duplicates (4)
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
module RSpec
  module Mocks
    RSpec.describe TestDouble do
      describe "#freeze" do
        subject { double }

        it "gives a warning" do
          expect(RSpec).to receive(:warn_with).with(/freeze a test double/)
          subject.freeze
        end

        it "gives the correct call site for the warning" do
          expect_warning_with_call_site(__FILE__, __LINE__ + 1)
          subject.freeze
        end

        it "doesn't freeze the object" do
          allow(RSpec).to receive(:warn_with).with(/freeze a test double/)
          double.freeze
          allow(subject).to receive(:hi)

          expect {
            subject.hi
          }.not_to raise_error
        end

        it "returns the instance of the test double" do
          allow(RSpec).to receive(:warn_with).with(/freeze a test double/)
          expect(subject.freeze).to eq subject
        end
      end

      RSpec.shared_examples_for "a copy method" do |method|
        it "copies the `as_null_object` state when #{method}'d" do
          dbl = double.as_null_object
          copy = dbl.__send__(method)
          expect(copy.foo.bar).to be(copy)
        end
      end

      include_examples "a copy method", :dup
      include_examples "a copy method", :clone

      [[:should, :expect], [:expect], [:should]].each do |syntax|
        context "with syntax #{syntax.inspect}" do
          include_context "with syntax", syntax

          it 'stubs the methods passed in the stubs hash' do
            dbl = double("MyDouble", :a => 5, :b => 10)

            expect(dbl.a).to eq(5)
            expect(dbl.b).to eq(10)
          end
        end
      end
    end
  end
end