Skip to content

Instantly share code, notes, and snippets.

@rubyandcoffee
Created August 24, 2016 13:49
Show Gist options
  • Save rubyandcoffee/0465387f1472bd5f12510f45405cfa75 to your computer and use it in GitHub Desktop.
Save rubyandcoffee/0465387f1472bd5f12510f45405cfa75 to your computer and use it in GitHub Desktop.
Spec for contacts
require 'spec_helper'
describe Contact do
it "has a valid factory" do
expect(build(:contact)).to be_valid
end
it { should validate_presence_of :first_name }
it { should validate_presence_of :last_name }
it { should validate_presence_of :email }
it { should validate_uniqueness_of(:email) }
it "returns a contact's full name as a string" do
contact = build_stubbed(:contact,
first_name: "Jane", last_name: "Doe")
expect(contact.name).to eq "Jane Doe"
end
describe "filter last name by letter" do
let(:smith) { create(:contact,
last_name: 'Smith', email: 'jsmith@example.com') }
let(:jones) { create(:contact,
last_name: 'Jones', email: 'tjones@example.com') }
let(:johnson) { create(:contact,
last_name: 'Johnson', email: 'jjohnson@example.com') }
context "matching letters" do
it "returns a sorted array of results that match" do
expect(Contact.by_letter("J")).to eq [johnson, jones]
end
end
context "non-matching letters" do
it "returns a sorted array of results that match" do
expect(Contact.by_letter("J")).to_not include smith
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment