I have an index action in a rails controller called images_controller.rb that fetches images from an external service. I am trying to write cucumber scenarios and am struggling with how to write/stub out my visit index page step. How could I stub out the images it fetches without it making the request?
Feature:
Scenario: Image Index
Given an image exists on the image server
When I am on the images page
Then I should see images
Steps so far:
Given(/^an image exists on the image server$/) do
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 am on the images page$/) do
body = "[{\"image/development_app\":[\"the_pistol.jpeg\"]},{\"image/development_can\":[\"kaepernick.jpg\"]}]"
@images = JSON.parse(body)
end
Then(/^I should see images$/) do
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 are "full-stack integration tests", meaning you really don't want to stub anything out unless absolutely necessary. If you do stub those calls out, how would you know if all of a sudden the external service stopped behaving the way you expect it to?
That said, in order to stub it, you'd want to stub the method
get
on the object returned by the controller methodimage_server_connection
.