Sha256: d5f7ce64ef8cc304f739e644da810f097c76754ec494f5840dcc69cd1a4b8c09

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

require 'spec_helper'

describe 'Cistern::Collection' do
  class SampleService < Cistern::Service
  end

  class Drug < SampleService::Model
    identity :id
    attribute :name
  end

  class Drugs < SampleService::Collection
    model Drug

    def all
      load([{ id: 1 }, { id: 3, name: 'tom' }, { id: 2 }])
    end
  end

  class Tacs < SampleService::Collection
    service_method :toes
  end

  it 'should generate a default collection method' do
    expect(SampleService.new.drugs).not_to be_empty
  end

  it 'should allow for a specific collection name' do
    expect(SampleService.new).to respond_to(:toes)
    expect(SampleService.new).not_to respond_to(:tacs)
  end

  it 'should give to_s' do
    collection = Drugs.new
    expect(collection.to_s).not_to eq '[]'
    expect(collection.to_s.gsub(/:[^>]*/, '')).to eq(collection.all.to_s.gsub(/:[^>]*/, ''))
  end

  it 'should give size and count' do
    expect(Drugs.new.size).to eq(3)
    expect(Drugs.new.count).to eq(3)
  end

  it 'should give first' do
    expect(Drugs.new.first).to eq(Drug.new(id: 1))
  end

  it 'should give last' do
    expect(Drugs.new.last).to eq(Drug.new(id: 2))
  end

  it 'should reject' do
    expect(Drugs.new.reject { |m| m.id == 2 }).to eq([Drug.new(id: 1), Drug.new(id: 3)])
  end

  it 'should select' do
    expect(Drugs.new.select { |m| m.id == 2 }).to eq([Drug.new(id: 2)])
  end

  it 'should slice' do
    expect(Drugs.new.slice(0, 2)).to eq([Drug.new(id: 1), Drug.new(id: 3, name: 'tom')])
  end

  it 'should ==' do
    Drugs.new.all == Drugs.new.all
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cistern-2.2.7 spec/collection_spec.rb
cistern-2.2.6 spec/collection_spec.rb
cistern-2.2.5 spec/collection_spec.rb
cistern-2.2.4 spec/collection_spec.rb