Skip to content

Instantly share code, notes, and snippets.

@paulfioravanti
Created July 2, 2015 12:26
Show Gist options
  • Save paulfioravanti/a652abcfc67fb8a6d748 to your computer and use it in GitHub Desktop.
Save paulfioravanti/a652abcfc67fb8a6d748 to your computer and use it in GitHub Desktop.
Presence Validator Test
require 'active_record'
require 'rspec'
require 'shoulda-matchers'
class Baz < ActiveRecord::Base
establish_connection(adapter: 'sqlite3', database: ':memory:')
validates_presence_of :foo, unless: :bar
def bar
# true or false
end
end
ActiveRecord::Schema.define do
@connection = Baz.connection
create_table :bazs do |t|
t.string :foo
end
end
RSpec.describe Baz do
let(:baz) { described_class.new }
context 'when #bar is true' do
before do
allow(baz).to receive(:bar).and_return(true)
end
it 'does not validate presence of foo' do
expect(baz).to_not validate_presence_of(:foo)
end
end
context 'when #bar is false' do
before do
allow(baz).to receive(:bar).and_return(false)
end
it 'validates presence of foo' do
expect(baz).to validate_presence_of(:foo)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment