Question

I have an app that relies on a 3rd party API called PSC, but I want to isolate my cucumber tests from API calls to PSC.

So, I wrote a couple of cucumber steps:

When /^we pretend that PSC is up$/ do
  PscV1.default_psc_connection("test user").stub!(:default_connection_is_up?).and_return(true)
end

When /^we pretend like PSC assignments exist for all subjects$/ do
  PscV1.default_psc_connection("test user").stub!(:assignment_exists?).and_return(true)
end

...and what these stubs are supposed to do is make the Cucumber scenario think that the API calls are working. However, the stubs don't seem to persist between steps, so further steps in my scenario don't get the stubbed return values, they try to make an actual API call, and therefore they fail.

Is there a way to get stubs to persist at least as long as an entire scenario? I've used stubs successfully in other Cucumber tests, so I know they work in general, but this is the first time I've written a Cucumber step whose entire purpose is to provide a stub.

Was it helpful?

Solution

As far as I can tell, the answer to whether or not they persist is, quite simply, "no".

I wound up writing a combined step that did the following:

When /^I follow "([^\"]*)" while pretending that PSC is up and assignments exists for all users$/ do |link_text|
  PscV1.stub!(:default_connection_is_up?).and_return(true)
  PscV1.default_psc_connection("test user").stub!(:assignment_exists?).and_return(true)
  click_link link_text
end

...which works. It doesn't allow me to reuse the stubs, as their own steps, unfortunately, but it works.

UPDATE You might be able to get around this limitation by assigning the stub to a class level variable, which is accessible from other steps within the same scenario.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top