【问题标题】:Testing dynamic attributes with cucumber, undefined method用黄瓜测试动态属性,未定义的方法
【发布时间】:2012-08-30 12:47:37
【问题描述】:

我们有一个带有dynamic attributesStyle 模型,可以通过用属性键填充一个字段并用值填充下一个字段来保存它。

典型的 params 哈希如下所示:

{"utf8"=>"✓", "style"=>{"collection_id"=>"48", "program_id"=>"989", "number"=>"454632", "name"=>"t67f", "category_id"=>"19", "field_KEY"=>"VALUE"}, "commit"=>"save", "id"=>"4521"}

点击它时会按预期工作,"field_KEY" => "VALUE" 对使用 getter(field_KEY) 和 setter(field_KEY=) 方法创建一个新的动态属性。

问题是:如果这个过程是用黄瓜模拟的,在属性设置之前,有些东西会调用哈希中所有键的getter,包括field_KEY

普通属性将返回 nil 以获取新记录,但由于尚未创建 field_KEY 的 getter,这将导致

`UndefinedMethodError: undefined method 'field_KEY'`.

现在我的问题是:您是愿意追踪field_KEY getter 的调用者并搞乱黄瓜,还是应该尝试模拟一个假方法,例如:

def check_method(method_name)
    if method_name =~ /^field_/
       nil
    else
       ... # let the Error be raised
end

欢迎更好的想法或解决方案

谢谢

【问题讨论】:

  • 使用 --backtrace 运行 cucumber 肯定会很快暴露查询不存在属性的代码?
  • 感谢您的帮助,我找到了。单击“保存”按钮后,检查模型是否使用正确的属性创建是一个腌制步骤。由于我们使用 Rspec 对此进行了介绍,因此我现在只需在页面重新加载后检查字段是否包含正确的值。

标签: ruby-on-rails-3 cucumber dynamic-attributes


【解决方案1】:

问题是:

field_KEY 的调用来自pickle,因为我包含了该步骤

And the style's "field_KEY" should be "VALUE"

看起来像这样:

Then(/^#{capture_model}'s (\w+) (should(?: not)?) be #{capture_value}$/) do |name, attribute, expectation, expected|
  actual_value  = model(name).send(attribute)
  expectation   = expectation.gsub(' ', '_')

  case expected
  when 'nil', 'true', 'false'
    actual_value.send(expectation, send("be_#{expected}"))
  when /^[+-]?[0-9_]+(\.\d+)?$/
    actual_value.send(expectation, eql(expected.to_f))
  else
    actual_value.to_s.send(expectation, eql(eval(expected)))
  end
end

我仍然不知道为什么到目前为止还没有创建 dynamic_attribute getter。

我最终做了什么:

在我看来(也解决了问题;)),黄瓜测试应该是黑盒测试,这就是为什么我选择改变步骤,现在我使用

And   the "key1" field should contain "KEY"

在页面重新加载后检查字段是否填充了正确的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多