0

動的属性を持つStyleモデルがあります。これは、1つのフィールドに属性キーを入力し、次のフィールドに値を入力することで保存できます。

典型的な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=)メソッドを使用して新しい動的属性を作成します。

問題は次のとおりです。プロセスがキュウリでシミュレートされている場合、属性が設定される前に、ハッシュ内のすべてのキーに対してゲッターが呼び出されますfield_KEY

通常の属性はnil新しいレコードに対して返されますが、のゲッターfield_KEYはまだ作成されていないため、結果として

`UndefinedMethodError: undefined method 'field_KEY'`.

さて、私の質問です。ゲッターの発信者を追跡しfield_KEYてキュウリをいじりまわすのか、それとも次のような偽のメソッドをシミュレートするのか。

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

より良いアイデアや解決策は大歓迎です

ありがとう

4

1 に答える 1

1

The Problem was:

The call to field_KEY came from pickle, because I included the step

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

which looks like this:

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

I still don't know why the dynamic_attribute getter had not been created up to this point.

What I ended up doing:

In my opinion (also, it solved the problem ;)), cucumber tests should be black-box tests, thats why I chose to change the steps and now I use

And   the "key1" field should contain "KEY"

which checks if the field has been filled with the correct value after the page reloads.

于 2012-08-30T14:44:26.920 に答える