Skip to content

Instantly share code, notes, and snippets.

@ogredude
Created July 4, 2011 22:39
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/1064019 to your computer and use it in GitHub Desktop.
Save ogredude/1064019 to your computer and use it in GitHub Desktop.
class Item < ActiveRecord::Base
has_many :orders, :through => :line_items
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_many(:orders).through(:line_items)}
[: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 LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :item
end
Failures:
1) Item
Failure/Error: it {should have_many(:orders).through(:line_items)}
Expected Item to have a has_many association called orders (Item does not have any relationship to line_items)
# ./spec/models/item_spec.rb:4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment