Skip to content

Instantly share code, notes, and snippets.

@mhuerster
Created August 6, 2014 02:17
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 mhuerster/d0a1add9f4ec9698e78b to your computer and use it in GitHub Desktop.
Save mhuerster/d0a1add9f4ec9698e78b to your computer and use it in GitHub Desktop.
Advanced AR Challenge with shoulda-matchers
require_relative 'bookings'
require 'rspec'
require 'shoulda-matchers'
describe "Hotel Bookings" do
before do
@francis = User.find_by(name: "Francis Slim")
@julie = User.find_by(name: "Julie Blook")
@mike = User.find_by(name: "Mike Rasta")
@ritz = Hotel.find_by(name: "Ritz")
@westin = Hotel.find_by(name: "Westin")
end
describe User do
context "base record examples" do
it "three users with correct names should exist in the database" do
expect(User.order(:name).map(&:name)).to eq(["Francis Slim", "Julie Blook", "Mike Rasta"])
end
end
end
describe Hotel do
context "shoulda-matchers" do
it { should have_many(:rooms) }
it { should have_many(:bookings) }
it { should have_many(:booked_guests).source(:guest) }
end
context "base record examples" do
it "two hotels Westin and Ritz should exist in the database" do
expect(Hotel.order(:name).map(&:name)).to eq(["Ritz", "Westin"])
end
it "the Westin hotel should have 5 rooms" do
expect(@westin.rooms.count).to eq(5)
end
it "the Westin hotel's room's should all cost $300" do
expect(@westin.rooms.map(&:rate).uniq).to eq([300])
end
it "the Ritz hotel should have 3 rooms" do
expect(@ritz.rooms.count).to eq(3)
end
it "the Ritz hotel's room's should all cost $500" do
expect(@ritz.rooms.map(&:rate).uniq).to eq([500])
end
end
end
describe Room do
context "shoulda-matchers" do
it { should have_many(:bookings) }
it { should belong_to(:hotel) }
end
end
describe Booking do
context "shoulda-matchers" do
it { should belong_to(:room) }
it { should belong_to(:guest).class_name('User').with_foreign_key('user_id') }
end
context "base record examples" do
it "Francis should have two bookings" do
expect(@francis.bookings.count).to eq(2)
end
it "Francis should have one booking at the Ritz" do
expect(@ritz.booked_guests).to include(@francis)
end
it "Francis should have 1 booking at the Westin" do
expect(@westin.booked_guests).to include(@francis)
end
it "Julie should have 1 booking at the Ritz" do
expect(@ritz.booked_guests).to include(@julie)
end
it "Mike should have 2 bookings at the Westin" do
expect(@westin.booked_guests).to include(@mike)
end
it "Mike's bookings should be marked as paid" do
expect(@mike.bookings.all?(&:paid)).to be true
end
it "the Westin should have 3 bookings" do
expect(@westin.bookings.count).to eq(3)
end
it "the Ritz should have 2 bookings" do
expect(@ritz.bookings.count).to eq(2)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment