【问题标题】:How to pass multiple arguments in Ruby?如何在 Ruby 中传递多个参数?
【发布时间】:2015-02-17 16:06:41
【问题描述】:

我是 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

我已经创建了一个这样的 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 的 Cucumber and Cheese

【问题讨论】:

  • 试试@cart.name_for_line_item(line_item).should include(puppy_name)
  • 只需include.should include
  • 它给出了错误.rb:12: syntax error, unexpected ',', expecting ')' @cart.name_for_line_item include (puppy_name, line_item)

标签: ruby rspec cucumber watir


【解决方案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

【讨论】:

  • #first 给了NoMethodError: undefined method include?' for #RSpec::Matchers::BuiltIn::PositiveOperatorMatcher:0x2e98c90>,但第二个就像魅力一样。这么快的回复。
猜你喜欢
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 2012-11-27
  • 2013-11-21
  • 2012-06-12
  • 1970-01-01
  • 2019-03-21
相关资源
最近更新 更多