私たちは、Rails 3.2アプリケーションから多くのフィールドにattr_accessible
とattr_protected
を設定しています。今のところ、これらのフィールドが保護されていることを確認するためのテストは実際にはありません。RSpecのattr_accessibleフィールドのテスト方法
だから私はいくつかの答えをグーグルすることを決定し、この溶液につまずい:
RSpec::Matchers.define :be_accessible do |attribute|
match do |response|
response.send("#{attribute}=", :foo)
response.send("#{attribute}").eql? :foo
end
description { "be accessible :#{attribute}" }
failure_message_for_should { ":#{attribute} should be accessible" }
failure_message_for_should_not { ":#{attribute} should not be accessible" }
end
しかし、このソリューションは、テストの方法が応答しているかどうかを確認します。私が必要とするのは、アトリビュートがマスに割り当てられることができるかどうかをテストする方法です。私は正直な構文を愛し
it { should_not be_accessible :field_name }
it { should be_accessible :some_field }
は、誰もがこの問題に対するより良い解決策を持っていますか?属性が#accessible_attributes
リスト
RSpec::Matchers.define :be_accessible do |attribute|
match do |response|
response.class.accessible_attributes.include?(attribute)
end
description { "be accessible :#{attribute}" }
failure_message_for_should { ":#{attribute} should be accessible" }
failure_message_for_should_not { ":#{attribute} should not be accessible" }
end
[Permitters]使用を検討してください(https://github.com/permitters/permitters) (attr_accessible + attr_protected)の代わりに[Strong Parameters](https://github.com/rails/strong_parameters)を使用してください。 –
すでに実動環境で使用しています。これは、ForbiddenAttributesが主流ではないときからのものでした。 – WarmWaffles