Sha256: ab4bc2ebe1f570268d9f2536087e164f55402a51da1bba9b19423785711bef5b

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require 'spec_helper'

describe Neighborhood do
  it { is_expected.to be_a_kind_of InscriptioCursualis::Neighborhood }

  it { is_expected.to belong_to(:city) }
  it { is_expected.to belong_to(:district) }

  it { is_expected.to have_and_belong_to_many(:streets) }

  it { is_expected.to validate_presence_of :name }
  it { is_expected.to validate_presence_of :city }

  context 'district' do
    context 'with a city' do
      before do
        allow(subject).to receive(:city).and_return(city)
      end

      let :city do
        double(:city, districts: [inside_district], marked_for_destruction?: false)
      end

      let :inside_district do
        double(:district)
      end

      let :outside_district do
        double(:district)
      end

      context 'inside district' do
        it 'should be valid' do
          allow(subject).to receive(:district).and_return(inside_district)

          subject.valid?

          expect(subject.errors[:district]).to be_empty
        end
      end

      context 'outside district' do
        it 'should be invalid' do
          allow(subject).to receive(:district).and_return(outside_district)

          subject.valid?

          expect(subject.errors[:district]).to include 'is invalid'
        end
      end
    end

    context 'without a city' do
      let :district do
        double(:district)
      end

      it 'should not include error on district if has not a city' do
        allow(subject).to receive(:district).and_return(district)

        subject.valid?

        expect(subject.errors[:district]).to_not include 'is invalid'
      end
    end
  end

  it 'cast to string using name' do
    subject.name = 'Centro'

    expect(subject.to_s).to eq 'Centro'
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unico-training-7.8.0 spec/models/neighborhood_spec.rb