Skip to content

Instantly share code, notes, and snippets.

@yihyang
Last active August 21, 2018 17:51
Show Gist options
  • Select an option

  • Save yihyang/27569332d1b0bcfda05b119005e80d6f to your computer and use it in GitHub Desktop.

Select an option

Save yihyang/27569332d1b0bcfda05b119005e80d6f to your computer and use it in GitHub Desktop.
Rspec testing validator
require 'rails_helper'
class Validatable
include ActiveModel::Validations
attr_accessor :data
validates :data, lead_data: true
end
describe DataValidator do
subject { Validatable.new }
before { subject.data = data }
context 'valid data' do
let(:data) do
{ 'product' => 'Valid Product' }
end
it { is_expected.to be_valid }
end
context 'contains invalid data' do
let(:data) do
{ 'product' => 'Invalid Product' }
end
it { is_expected.to be_invalid }
end
context 'empty data' do
let(:data) { {} }
it { is_expected.to be_invalid }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment