Sha256: 07b4d3251e906effa736c0291ccb968f25b8da10cd6f1ee40b5d45af646cfab1

Contents?: true

Size: 1.76 KB

Versions: 41

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module BetterRecord
  module Rspec
    module Extensions
      def boolean_column(factory_name, column_name, default_val = false)
        describe column_name.to_s do
          let(:record) { build(factory_name) }

          it "cannot be nil in the database" do
            stubbed = record.dup
            allow(stubbed).to receive(:set_booleans)

            stubbed.__send__"#{column_name}=", nil
            expect(stubbed.valid?).to be false
            expect(stubbed.errors[column_name]).to include("must be true or false")
            expect { stubbed.save(validate: false) }.to raise_error(ActiveRecord::NotNullViolation)
          end

          it "defaults to '#{default_val}'" do
            empty_record = record.class.new
            expect(empty_record.__send__ column_name).to be default_val
          end

          it "will parse to boolean on save" do
            record.__send__"#{column_name}=", nil
            record.save
            expect(record.__send__ column_name).to be false

            record.__send__"#{column_name}=", 0
            record.save
            expect(record.__send__ column_name).to be false

            record.__send__"#{column_name}=", 'false'
            record.save
            expect(record.__send__ column_name).to be false

            record.__send__"#{column_name}=", 1
            record.save
            expect(record.__send__ column_name).to be true

            record.__send__"#{column_name}=", 'asdf'
            record.save
            expect(record.__send__ column_name).to be true

            record.__send__"#{column_name}=", 'true'
            record.save
            expect(record.__send__ column_name).to be true

            record.destroy
          end
        end
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
better_record-0.16.1 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.16.0 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.15.2 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.15.1 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.15.0 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.14.6 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.14.5 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.14.4 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.14.3 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.14.2 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.14.1 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.14.0 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.13.1 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.13.0 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.12.5 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.12.4 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.12.3 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.12.2 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.12.1 lib/better_record/rspec/extensions/boolean_column.rb
better_record-0.11.5 lib/better_record/rspec/extensions/boolean_column.rb