ruby on rails - Cucumber capybara stubbing -
ruby on rails - Cucumber capybara stubbing -
i have index action in rails controller called images_controller.rb fetches images external service. trying write cucumber scenarios , struggling how write/stub out visit index page step. how stub out images fetches without making request?
feature:
scenario: image index given image exists on image server when on images page should see images
steps far:
given(/^an image exists on image server$/) image_server_url = image_server['base_url'] + "/all_images" image = "image.png" image_path = "development_can/image.png" response = [{image_path => [image]}].to_json stub_request(:get, image_server_url).to_return(json.parse(response)) end when(/^i on images page$/) body = "[{\"image/development_app\":[\"the_pistol.jpeg\"]},{\"image/development_can\":[\"kaepernick.jpg\"]}]" @images = json.parse(body) end then(/^i should see images$/) end
controller:
class imagescontroller < applicationcontroller def index response = image_server_connection.get(image_server['base_url'] + "/all_images") @images = json.parse(response.body) end end
generally speaking, cucumber specs "full-stack integration tests", meaning don't want stub out unless absolutely necessary. if stub calls out, how know if of sudden external service stopped behaving way expect to?
that said, in order stub it, you'd want stub method get
on object returned controller method image_server_connection
.
ruby-on-rails ruby cucumber capybara
Comments
Post a Comment