Spec sample

C++ code posted
created at 20 Feb 15:18, updated at 05 Mar 08:43

Edit | Back
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
describe 'Object#stub' do
  before do
    @array = %w(aardvark bonobo chipmunk dromedary)
  end

  it 'can stub a method within the scope of a block' do
    @array.length.must_equal 4

    @array.stub(:length, 10) do
      @array.length.must_equal 10
    end

    @array.length.must_equal 4
  end

  it 'can stub a method using a callable object to generate return values' do
    callable = -> { 'ant' }
    @array.stub(:shift, callable) do
      @array.shift.must_equal 'ant'
      @array.shift.must_equal 'ant'
    end

    @array.shift.must_equal 'aardvark'
    @array.shift.must_equal 'bonobo'
  end

  describe 'stubbing a non-existent method' do
    it 'raises an error' do
      -> { @array.stub(:foo, :bar) { } }.must_raise NameError
    end
  end
end
790 Bytes in 2 ms with coderay