如何在Ruby中传递多个参数?

时间:2014-12-18 19:05:11

标签: ruby rspec cucumber watir

我是Ruby的新手,无法从这里获得任何答案,因为代码级别在这里看起来非常先进:

  1. How do I pass multiple arguments to a ruby method as an array?

  2. Ruby method with maximum number of parameters

  3. http://www.tutorialspoint.com/ruby/ruby_methods.htm

  4. 我创建了一个这样的step_definitions:

    Then(/^I should see "([^"]*)" as the name for line item (.*)$/) do |puppy_name, line_item|
      @cart.name_for_line_item line_item
    end
    

    其中方法name_for_line_item仅采用一个参数,即line_item
    但在上面的step_definition中,我必须验证我正在传递的puppy_name

    所以我试过了:

    @cart.name_for_line_item.should include puppy_name line_item但这是方法下的红线错误

    如果我尝试

    @cart.name_for_line_item line_item .should include puppy_name它给出了编译时错误:

    RSpec::Expectations::ExpectationNotMetError

    来源:Jeff Morgan的黄瓜和奶酪

1 个答案:

答案 0 :(得分:1)

你应该使用括号:

(@cart.name_for_line_item(line_item).should).include? puppy_name 

最新版本的rspec允许另一种语法:

expect(@cart.name_for_line_item(line_item)).to include(puppy_name)

链接到rspec文档:https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers