Created
July 4, 2011 22:39
-
-
Save ogredude/1064019 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LineItem < ActiveRecord::Base | |
belongs_to :order | |
belongs_to :item | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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