1.spec/support/be_accessible_matcher.rb を作成
be_accessible_matcher.rb
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
2.モデルのスペックを作成(Userの場合)
user_spec.rb
# encoding: utf-8
require 'spec_helper'
describe User do
context "attr_accessible が設定されている" do
before do
@user = User.new
@accessible = [:firstname, :lastname, :email]
end
it "すべてのカラムをチェック" do
@event.class.column_names.each do |name|
if @accessible.include?(name.to_sym) then
@event.should be_accessible name.to_sym
else
@event.should_not be_accessible name.to_sym
end
end
end
end
end