本文介绍了黄瓜水ara的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为images_controller.rb的rails控制器中有一个索引操作,它从外部服务获取图像。我试图写黄瓜方案,并努力与如何写/存根我访问索引页步骤。



功能:

 场景:图像索引
给定一个图像存在于图像服务器上
当我在图像页面
然后我应该看到图像

到目前为止的步骤:

  (/ ^图像服务器上存在一个图像$ /)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

当page $ /)do
body =[{\image / development_app\:[\the_pistol.jpeg\]},{\image / development_can\:[ \\kaepernick.jpg\]}]
@images = JSON.parse(body)
end

然后(/ ^我应该看到图像$ /)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


解决方案

一般来说,全栈集成测试,这意味着你真的不想stub任何东西,除非绝对必要。如果你做了stub这些电话,你怎么知道突然外部服务是否停止了你期望的方式?



也就是说,为了stub it,你需要在由控制器方法 image_server_connection 返回的对象上存根方法 get p>

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 method image_server_connection.

这篇关于黄瓜水ara的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 18:58