그래서 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 }
누구든지이 문제에 대해 더 좋은 해결책을 가지고 있는가? 나는 비슷한 뭔가를 찾고 한 후 내가 했어야 - 정규의 allow_mass_assigment_of에 대해 들었다
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