黄瓜水豚茬

我在一个名为images_controller.rb的rails控制器中有一个索引操作,它从外部服务中获取图像。 我正在尝试编写黄瓜场景,并且正在努力解决如何编写/存根我的访问索引页面步骤。 如果不提出请求,我怎么能将它取出的图像存根?

特征:

Scenario: Image Index Given an image exists on the image server When I am on the images page Then I should see images 

目前为止的步骤:

 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 

控制器:

 class ImagesController < ApplicationController def index response = image_server_connection.get(IMAGE_SERVER['base_url'] + "/all_images") @images = JSON.parse(response.body) end end 

一般来说,Cucumber规范是“全栈集成测试”,这意味着除非绝对必要,否则你真的不想删除任何东西。 如果您确实将这些调用存根,那么您如何知道外部服务是否突然停止了您期望的行为?

也就是说,为了存根它,你想要将控制器方法image_server_connection返回的对象的方法image_server_connection