【问题标题】:Cucumber capybara stubbing黄瓜水豚存根
【发布时间】:2014-06-19 15:07:19
【问题描述】:

我在名为 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

【问题讨论】:

    标签: ruby-on-rails ruby cucumber capybara


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      相关资源
      最近更新 更多