Sha256: 023049244043a342d1b65e36704de10373d10198ce0269e12184a3f2566fbdf8

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

require "spec_helper"

describe Membrane::Schema::Record do
  describe "#validate" do
    it "should return an error if the validated object isn't a hash" do
      schema = Membrane::Schema::Record.new(nil)

      expect_validation_failure(schema, "test", /instance of Hash/)
    end

    it "should return an error for missing keys" do
      key_schemas = { "foo" => Membrane::Schema::ANY }
      rec_schema = Membrane::Schema::Record.new(key_schemas)

      expect_validation_failure(rec_schema, {}, /foo => Missing/)
    end

    it "should validate the value for each key" do
      data = {
        "foo" => 1,
        "bar" => 2,
      }

      key_schemas = {
        "foo" => mock("foo"),
        "bar" => mock("bar"),
      }

      key_schemas.each { |k, m| m.should_receive(:validate).with(data[k]) }

      rec_schema = Membrane::Schema::Record.new(key_schemas)

      rec_schema.validate(data)
    end

    it "should return all errors for keys or values that didn't validate" do
      key_schemas = {
        "foo" => Membrane::Schema::ANY,
        "bar" => Membrane::Schema::Class.new(String),
      }

      rec_schema = Membrane::Schema::Record.new(key_schemas)

      errors = nil

      begin
        rec_schema.validate({ "bar" => 2 })
      rescue Membrane::SchemaValidationError => e
        errors = e.to_s
      end

      errors.should match(/foo => Missing key/)
      errors.should match(/bar/)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
membrane-0.0.2 spec/record_schema_spec.rb
membrane-0.0.1 spec/record_schema_spec.rb