Skip to content

Instantly share code, notes, and snippets.

@ogredude
Created July 4, 2011 21:15
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 ogredude/1063953 to your computer and use it in GitHub Desktop.
Save ogredude/1063953 to your computer and use it in GitHub Desktop.
An Order belongs to a Customer using the Person model.
An Order has and belongs to many Items.
Given a Person, I want to find all the items they bought.
The problem: How do I write a test for the last sentence in this spec?
Factory.define :person do |f|
f.first_name Faker::Name.first_name
f.last_name Faker::Name.last_name
f.middle_name Faker::Name.first_name
end
Factory.define :address do |f|
f.street Faker::Address.street_address
f.city Faker::Address.city
f.state Faker::Address.state_abbr
f.zip Faker::Address.zip
f.association :person
end
Factory.define :order do |f|
f.association :customer, :factory => :person
f.date Date.today
end
Factory.define :item do |f|
f.name Faker::Company.catch_phrase
f.description Faker::Lorem.paragraph
f.price rand(100) + rand
end
class Item < ActiveRecord::Base
has_and_belongs_to_many :orders
validates_presence_of :name, :price
before_save :round_price
def round_price
self.price = price.round(2)
end
end
require 'spec_helper'
describe Item do
it {should have_and_belong_to_many(:orders)}
[:name, :price].each do |attr|
it "must have a(n) #{attr}" do
i = Factory.build(:item, attr.to_sym => nil)
i.should_not be_valid
i.errors[attr].should_not be_nil
end
end
it "should store 2 decimals on price" do
i = Factory(:item, :price => 3.987)
i.price.should == 3.99
end
end
class Order < ActiveRecord::Base
belongs_to :customer, :class_name => "Person"
has_and_belongs_to_many :items
validate :requires_at_least_one_item
def requires_at_least_one_item
errors.add(:items, "requires at least one Item") if items.empty?
end
end
require 'spec_helper'
describe Order do
it {should belong_to(:customer)}
it {should have_and_belong_to_many(:items)}
it "should have at least 1 item" do
o = Factory.build(:order, :items => [])
o.should_not be_valid
o.errors[:items].should_not be_empty
end
end
class Person < ActiveRecord::Base
has_many :addresses
has_many :orders, :as => :customer
validates_presence_of :first_name, :last_name
def full_name
[first_name, middle_name, last_name].delete_if{|a| a.blank?}.join(" ")
end
end
require 'spec_helper'
describe Person do
it "must have a first name" do
person = Factory.build(:person, :first_name => nil)
person.should_not be_valid
person.errors[:first_name].should_not be_empty
end
it "must have a last name" do
person = Factory.build(:person, :last_name => nil)
person.should_not be_valid
person.errors[:last_name].should_not be_empty
end
it "makes a full name from a first and last name" do
person = Factory.build(:person, :middle_name => nil)
person.full_name.should == person.first_name + " " + person.last_name
end
it "has an optional middle name" do
person = Factory.build(:person, :middle_name => "Englebert")
person.middle_name.should == "Englebert"
end
it "should make a full name from a first, last, and middle name" do
person = Factory.build(:person)
person.full_name.should == [person.first_name, person.middle_name, person.last_name].join(" ")
end
it "should save to the database when everything is valid" do
lambda {
Factory(:person)
}.should change(Person, :count).by(1)
end
it { should have_many(:addresses) }
it {should have_many(:orders)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment