LoginSignup
3
3

More than 5 years have passed since last update.

attr_accessibleのspec

Last updated at Posted at 2013-01-06
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

参考
RSpecでmodelのattr_accessibleをテストする

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3