File: precise_counts_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 (68 lines) | stat: -rw-r--r-- 2,155 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
require 'spec_helper'

module RSpec
  module Mocks
    describe "PreciseCounts" do
      before(:each) do
        @double = double("test double")
      end

      it "fails when exactly n times method is called less than n times" do
        @double.should_receive(:do_something).exactly(3).times
        @double.do_something
        @double.do_something
        expect {
          verify @double
        }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "fails fast when exactly n times method is called more than n times" do
        @double.should_receive(:do_something).exactly(3).times
        @double.do_something
        @double.do_something
        @double.do_something
        expect {
          @double.do_something
        }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "fails when exactly n times method is never called" do
        @double.should_receive(:do_something).exactly(3).times
        expect {
          verify @double
        }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "passes if exactly n times method is called exactly n times" do
        @double.should_receive(:do_something).exactly(3).times
        @double.do_something
        @double.do_something
        @double.do_something
        verify @double
      end

      it "returns the value given by a block when the exactly once method is called" do
        @double.should_receive(:to_s).exactly(:once) { "testing" }
        expect(@double.to_s).to eq "testing"
        verify @double
      end

      it "passes mutiple calls with different args" do
        @double.should_receive(:do_something).once.with(1)
        @double.should_receive(:do_something).once.with(2)
        @double.do_something(1)
        @double.do_something(2)
        verify @double
      end

      it "passes multiple calls with different args and counts" do
        @double.should_receive(:do_something).twice.with(1)
        @double.should_receive(:do_something).once.with(2)
        @double.do_something(1)
        @double.do_something(2)
        @double.do_something(1)
        verify @double
      end
    end
  end
end