Skip to content

Instantly share code, notes, and snippets.

@yesezra
Created August 3, 2012 05:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yesezra/3244678 to your computer and use it in GitHub Desktop.
Save yesezra/3244678 to your computer and use it in GitHub Desktop.
Testing custom ActiveRecord validators with rspec and with_model
class ExcitementValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /.*!/
record.errors.add attribute, (options[:message] || "must end with an exclamation point")
end
end
end
require 'spec_helper'
describe ExcitementValidator do
with_model :post do
table do |t|
t.string :title
end
model do
validates :title, excitement: true
end
end
it "should be valid with an excited title" do
Post.new(title: 'My amazing day!').should be_valid
end
it "should be invalid with an unexcited title" do
Post.new(title: 'My lousy day.').should_not be_valid
end
end
@praser
Copy link

praser commented May 5, 2018

Just what I was looking for. Tks.

@elceebee
Copy link

Thanks!

@heyjordn
Copy link

Bless you for posting this. Life saver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment