Cucumber 从一个步骤调用外部 ruby 函数?

Cucumber calling an external ruby function from a step?

我有一个 ruby 功能是这样写的:

#./features/sjsonTesting.feature
Feature: Validate DUT JSON
    JSON should be evaluated for all routes in API
    All API routes should return valid JSON
    If JSON is invalid for one or more route in API it has DUT failed 

Scenario Outline: Validate JSON
    Given there is a DUT with <IP> and <USERNAME> and <PASSWORD>
    Then it should return the word PASSED

  Examples:
    |IP             |USERNAME|PASSWORD|
    |'172.168.101.139'|admin   |test    |


I then have step definitions like:
#./features/step_definition/jsonSteps.rb
Given(/^there is a DUT with '(\d+)\.(\d+)\.(\d+)\.(\d+)' and admin and test$/) do |arg1, arg2, arg3, arg4|
  testdev(arg1)#pending # express the regexp above with the code you wish you had
end

Then(/^it should return the word PASSED$/) do
  testdev(arg1)#pending # express the regexp above with the code you wish you had
end


as well as a support file:
#./support/support.rb
#!/usr/bin/ruby
require 'uri'
require 'net/http'
require 'net/http/digest_auth'
require 'json' 
require 'rubygems' 


def is_json(json)
  begin
    JSON.parse(json.to_json)
    return true
  rescue Exception => e
    print e
    return false
  end
end



def gethash(route)
    digest_auth = Net::HTTP::DigestAuth.new
    uri = URI.parse route
    uri.user = 'admin'
    uri.password = 'terraceqam'
    h = Net::HTTP.new uri.host, uri.port
    h.use_ssl = true
    h.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req = Net::HTTP::Get.new uri.request_uri
    res = h.request req
    #puts res['www-authenticate']
    auth = digest_auth.auth_header uri, res['www-authenticate'], 'GET'
    #puts auth
    req = Net::HTTP::Get.new uri.request_uri
    #puts req
    req.add_field 'Authorization', auth
    res = h.request req
    #puts res.body
    data = JSON.parse(res.body)
    return data
end


def testdev(ip)
   test = "PASSED"
   hash = gethash('https://' + ip +'/views/')
   hash["views"].each do |view|
     routeapi = 'https://' + ip + '/views/' + view
     #print routeapi + "---" 
     subhash =  gethash(routeapi)
     answer = is_json(subhash)  
     if answer == false 
      test = "FAILED"
     end
   end
   return test
end

当我 运行 黄瓜时,我得到:

root@FPGA:/home/robm/code/BDD/testtq# cucumber
Feature: Validate DUT JSON
    JSON should be evaluated for all routes in API
    All API routes should return valid JSON
    If JSON is invalid for one or more route in API it has DUT failed

  Scenario Outline: Validate JSON                                # features/testJson.feature:6
    Given there is a DUT with <IP> and <USERNAME> and <PASSWORD> # features/step_definition/REST_Testing_Steps.rb:2
    Then it should return the word PASSED                        # features/step_definition/REST_Testing_Steps.rb:6

    Examples: 
      | IP                | USERNAME | PASSWORD |
      | '172.168.101.139' | admin    | test     |
      Invalid argument - connect(2) (Errno::EINVAL)
      ./features/support/testJson.rb:30:in `gethash'
      ./features/support/testJson.rb:46:in `testdev'
      ./features/step_definition/REST_Testing_Steps.rb:3:in `/^there is a DUT with '(\d+)\.(\d+)\.(\d+)\.(\d+)' and admin and test$/'
      features/testJson.feature:7:in `Given there is a DUT with <IP> and <USERNAME> and <PASSWORD>'

Failing Scenarios:
cucumber features/testJson.feature:6 # Scenario: Validate JSON

1 scenario (1 failed)
2 steps (1 failed, 1 skipped)
0m0.058s

它有问题 ./features/support/testJson.rb:30:in `gethash' 这是 res = h.request 请求

为什么我无法收到请求?在该帮助程序代码中,当我 运行 命令行中的代码时它工作正常。

更简单的方法是:

改为引号:

Examples: 
  | IP                | USERNAME | PASSWORD |
  | "172.168.101.139" | admin    | test     |

并匹配整个字符串

Given(/^there is a DUT with "(.*?)"$/) do |ip|
  puts  ip  #puts full IP address as string
end