Skip to content

Instantly share code, notes, and snippets.

@cjcolvar
Created June 18, 2013 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjcolvar/5806656 to your computer and use it in GitHub Desktop.
Save cjcolvar/5806656 to your computer and use it in GitHub Desktop.
spec/validators/uniqueness_validator_spec.rb
require 'spec_helper'
describe "UniquenessValidator" do
before(:all) do
@solr_field = "title_t"
@validator = UniquenessValidator.new({:attributes => {}, :solr_name => @solr_field})
@record = stub(pid:"avalon:2")
@record.stub("errors").and_return([])
@record.errors.stub('[]').and_return({})
@record.errors[].stub('<<')
end
it "should raise exception if solr_name option is missing" do
validator = UniquenessValidator.new({attributes: {}})
lambda {validator.validate_each(@record, "title", "new_title")}.should raise_error ArgumentError
end
it "should not return errors when field is unique" do
@record.should_not_receive('errors')
@validator.validate_each(@record, "title", "new_title")
end
it "should not return errors when field is unique but record is the same" do
doc = stub(pid: "avalon:2")
Unit.should_receive("where").once.with(@solr_field => "new_title").and_return(doc)
@record.should_not_receive('errors')
@validator.validate_each(@record, "title", "new_title")
end
it "should return erros when field is not unique" do
doc = stub(pid: "avalon:1")
Unit.should_receive("where").once.with(@solr_field => "old_title").and_return(doc)
@record.errors[].should_receive('<<')
@validator.validate_each(@record, "title", "old_title")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment