stubbing after_create

55 views
Skip to first unread message

fearless_fool

unread,
Feb 1, 2011, 7:41:39 PM2/1/11
to webmock-users
You could argue that it's a bad idea, but I've created a model that
accesses the web in its after_create method:

class Premise < ActiveRecord::Base
after_create :lookup_elevation
...
def lookup_elevation
(call google elevation service via Mechanize)
end
...
end

Of course, this immediately broke all of my RSpec tests, since
creating a Premise (even via FactoryGirl) will call lookup_elevation
which raises a WebMock::NetConnectNotAllowedError.

Is there a simple way to create a global stub? Or do I have to
sprinkle webmock stubs throughout my RSpec tests? (The latter would
make me sad, since it doesn't seem right to be stubbing web access in
tests that have nothing to to with accessing the web.)

TIA.

- ff

Bartosz Blimke

unread,
Feb 1, 2011, 8:02:33 PM2/1/11
to webmoc...@googlegroups.com
What about declaring the stub in a spec_helper.rb ?

Rspec.configure do |config|
  config.before :each do
    stub_request(.....)
  end
end

Bartosz

2011/2/2 fearless_fool <rdp...@gmail.com>

fearless_fool

unread,
Feb 5, 2011, 7:16:10 PM2/5/11
to webmock-users
On Feb 1, 5:02 pm, Bartosz Blimke <bartosz.bli...@gmail.com> wrote:
> What about declaring the stub in a spec_helper.rb ?
>
> Rspec.configure do |config|
>   config.before :each do
>     stub_request(.....)
>   end
> end
>
> Bartosz

Ah - perfect! I didn't know you could declare a stub like that. (Uh,
can you point me to documentation for Rpec.configure ... ?)

Thanks.

- ff
Reply all
Reply to author
Forward
0 new messages