Sha256: 1384adaec8c333c7bfdb68bd077f4144321f52857df5a7601b9242080e53a3aa

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

require 'spec_helper'

describe 'mock data' do
  class Patient < Cistern::Service
    request :diagnosis
    request :treat

    class Real
      def diagnosis(options={})
      end

      def treat(options={})
      end
    end

    class Mock
      def diagnosis(diagnosis)
        #self.data[:diagnosis] << rand(2) == 0 ? "sick" : "healthy"
        self.data.store(:diagnosis, self.data.fetch(:diagnosis) + [diagnosis])
      end

      def treat(treatment)
        self.data[:treatments] += [treatment]
      end
    end
  end

  shared_examples "mock_data#backend" do |backend, options|
    it "should store mock data" do
      Patient.mock!
      Patient::Mock.store_in(backend, options)
      Patient.reset!

      p = Patient.new
      p.diagnosis("sick")
      expect(p.data[:diagnosis]).to eq(["sick"])

      p.reset!

      expect(p.data[:diagnosis]).to eq([])

      p.treat("healthy")
      expect(p.data[:treatments]).to eq(["healthy"])

      Patient.reset!

      expect(p.data[:treatments]).to eq([])
    end

  end

  context "with a storage backend" do
    describe "Cistern::Data::Hash" do
      include_examples "mock_data#backend", :hash
    end

    describe "Cistern::Data::Redis" do
      include_examples "mock_data#backend", :redis

      context "with an explicit client" do
        before(:each) {
          @other = Redis::Namespace.new("other_cistern", Redis.new)
          @other.set("x", "y")
        }

        include_examples "mock_data#backend", :redis, client: Redis::Namespace.new("cistern", Redis.new)

        after(:each) {
          expect(@other.get("x")).to eq("y")
          @other.del("x")
        }
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
cistern-0.12.3 spec/mock_data_spec.rb
cistern-0.12.2 spec/mock_data_spec.rb
cistern-0.12.1 spec/mock_data_spec.rb
cistern-0.11.3 spec/mock_data_spec.rb
cistern-0.11.2 spec/mock_data_spec.rb
cistern-0.11.1 spec/mock_data_spec.rb
cistern-0.11.0 spec/mock_data_spec.rb
cistern-0.10.2 spec/mock_data_spec.rb
cistern-0.9.2 spec/mock_data_spec.rb
cistern-0.9.1 spec/mock_data_spec.rb
cistern-0.9.0 spec/mock_data_spec.rb
cistern-0.8.0 spec/mock_data_spec.rb
cistern-0.7.1 spec/mock_data_spec.rb
cistern-0.7.0 spec/mock_data_spec.rb