Skip to content

Instantly share code, notes, and snippets.

@hzm-s
Created September 3, 2012 02:50
Show Gist options
  • Save hzm-s/3606433 to your computer and use it in GitHub Desktop.
Save hzm-s/3606433 to your computer and use it in GitHub Desktop.
DRY'uped validator spec
require 'spec_helper'
shared_examples "valid" do
it { expect { subject }.not_to raise_error(Avalon::Error) }
end
shared_examples "invalid" do
it { expect { subject }.to raise_error(Avalon::Error) }
end
describe Avalon::Validator do
let(:validator) { described_class.new candidate, pattern }
let(:candidate) { "target" }
describe "#validate" do
subject { validator.validate }
context "given pattern as String", "when match" do
let(:pattern) { "target" }
it_behaves_like "valid"
end
context "given pattern as String", "when NOT match" do
let(:pattern) { "not matched" }
it_behaves_like "invalid"
end
context "given pattern as Regexp", "when match" do
let(:pattern) { /target/ }
it_behaves_like "valid"
end
context "given pattern as Regexp", "when NOT match" do
let(:pattern) { /not matched/ }
it_behaves_like "invalid"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment