Sha256: 146e2a0328cfc90d3ed8c2f17a6a8957fa3d58668ad4508007007d381919b806

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require 'spec_helper'
require 'timecop'

##
# Custom matcher that verifies whether the period represented by the start
# and end timestamps of the given model is close enough to the given duration.
RSpec::Matchers.define :have_duration_of do |duration|
  match do |actual|
    real_dur = actual.end_time - actual.start_time
    (real_dur - duration).abs < 0.1
  end
end

describe Krikri::Activity, type: :model do
  subject { create(:krikri_activity) }

  describe '#rdf_subject' do
    it 'is a URI' do
      expect(subject.rdf_subject).to be_a RDF::URI
    end

    it 'uses #id as local name ' do
      expect(subject.rdf_subject.to_s).to end_with subject.id.to_s
    end
  end

  describe 'start_time' do
    before do
      subject.set_start_time
    end

    it 'marks start time' do
      expect(subject.start_time).to be_a ActiveSupport::TimeWithZone
    end
  end

  describe 'end_time' do
    it 'raises an error if not started' do
      expect { subject.set_end_time }.to raise_error
    end
  end

  describe '#run' do
    it 'runs the given block' do
      expect { |b| subject.run(&b) }
        .to yield_with_args(subject.agent_instance, subject.rdf_subject)
    end

    it 'sets start and end times when running a block' do
      duration = 30     # seconds
      subject.run { Timecop.travel(duration) }
      Timecop.return    # come back to the present for future tests
      expect(subject).to have_duration_of(duration)
    end
  end

  describe '#agent_instance' do
    it 'returns an instance of the agent class' do
      expect(subject.agent_instance)
        .to be_an_instance_of(subject.agent.constantize)
    end

    it 'returns the same instance for successive calls' do
      expect(subject.agent_instance).to be subject.agent_instance
    end
  end

  describe '#parsed_opts' do
    it 'is a hash of opts' do
      expect(subject.parsed_opts).to be_a Hash
    end

    it 'has symbolized keys' do
      subject.parsed_opts.keys.each do |k|
        expect(k).to be_a Symbol
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
krikri-0.1.1 spec/models/activity_spec.rb
krikri-0.1.0 spec/models/activity_spec.rb