黄瓜水豚茬

时间:2014-06-19 15:07:19

标签: ruby-on-rails ruby cucumber capybara

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

特点:

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

1 个答案:

答案 0 :(得分:0)

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

那就是说,为了存根它,你想要在控制器方法get返回的对象上存根方法image_server_connection