Stub methods with newer syntax (stub that raises and then returns)

16 views
Skip to first unread message

Joery Moinsen

unread,
Mar 28, 2017, 4:33:33 PM3/28/17
to rspec
Hello, how can i stub methods with the newer syntax? My current stub looks like

      dbl.stub(:raise_then_return) do
        @counter = @counter.to_i.succ
        raise RuntimeError if @counter < 2
        return "foo"
      end

and i get a deprecation warning

Deprecation Warnings:

Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead.

The doc on Method stubs (https://www.relishapp.com/rspec/rspec-mocks/v/2-3/docs/method-stubs) has no equivalent for Rspec 3.5

What is the preferred way to express my stub? I need a method that raises on first call and then returns something on the second call.

Many thanks for considering my request.

Myron Marston

unread,
Mar 28, 2017, 4:34:46 PM3/28/17
to rs...@googlegroups.com

With the newer, non-monkey patching syntax, you would do:

allow(dbl).to receive(:raise_then_return) do

  @counter = @counter.to_i.succ
  raise RuntimeError if @counter < 2

  "foo"
end

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/eb57df04-8ab3-4503-8c4e-89ab5c468e4a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joery Moinsen

unread,
Mar 29, 2017, 12:49:21 AM3/29/17
to rspec
thank you very much for the quick response.
Reply all
Reply to author
Forward
0 new messages