黄瓜从一个步骤调用外部红宝石功能?

时间:2015-03-05 17:56:30

标签: ruby cucumber

我有一个像这样写的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:在'gethash&#39;是的     res = h.request req

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

1 个答案:

答案 0 :(得分:0)

更简单的方法是:

更改为引号:

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