File: test_double_spec.rb

package info (click to toggle)
ruby-rspec-mocks 2.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 868 kB
  • ctags: 725
  • sloc: ruby: 8,227; makefile: 4
file content (67 lines) | stat: -rw-r--r-- 2,032 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
require 'spec_helper'

module RSpec
  module Mocks
    describe TestDouble do
      before(:all) do
        Module.class_eval do
          private
          def use; end
        end
      end

      after(:all) do
        Module.class_eval do
          undef use
        end
      end

      it 'can be extended onto a module to make it a pure test double that can mock private methods' do
        double = Module.new
        double.stub(:use)
        expect { double.use }.to raise_error(/private method `use' called/)

        double = Module.new { TestDouble.extend_onto(self) }
        double.should_receive(:use).and_return(:ok)
        expect(double.use).to be(:ok)
      end

      it 'sets the test double name when a name is passed' do
        double = Module.new { TestDouble.extend_onto(self, "MyDouble") }
        expect { double.foo }.to raise_error(/Mock "MyDouble" received/)
      end

      [[: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
            double = Module.new do
              TestDouble.extend_onto(self, "MyDouble", :a => 5, :b => 10)
            end

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

      it 'indicates what type of test double it is in error messages' do
        double = Module.new do
          TestDouble.extend_onto(self, "A", :__declared_as => "ModuleMock")
        end
        expect { double.foo }.to raise_error(/ModuleMock "A"/)
      end

      it 'is declared as a mock by default' do
        double = Module.new { TestDouble.extend_onto(self) }
        expect { double.foo }.to raise_error(/Mock received/)
      end

      it 'warns of deprecation of :null_object => true' do
        RSpec.should_receive :deprecate
        double = Class.new { TestDouble.extend_onto self, 'name', :null_object => true }
      end
    end
  end
end