Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created December 14, 2010 01:52
Show Gist options
  • Save agmcleod/739893 to your computer and use it in GitHub Desktop.
Save agmcleod/739893 to your computer and use it in GitHub Desktop.
Model:
class Order < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
# PAYMENT_TYPES = ["Check", "Credit card", "Purchase order"]
belongs_to :payment_type
validates :name, :address, :email, :presence => true
validates_inclusion_of :payment_type_id, :in => PaymentType.all.map { |type| type.id }, :message => "- Invalid payment type selected"
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
end
Unit Test:
require 'test_helper'
class OrderTest < ActiveSupport::TestCase
fixtures :orders
fixtures :payment_types
setup do
@order = orders(:two)
end
test "order attributes must not be empty" do
order = Order.new
assert order.invalid?
[:name, :address, :email, :payment_type_id].each do |sym|
assert order.errors[sym].any?
end
# test fixture values
File.open(File.join(Rails.root, 'out.txt'), 'w+') do |file|
@order.errors.each_full {|msg| file.write "#{msg}\n"}
end
assert_equal @order.payment_type_id, payment_types(:check).id
assert_not_equal @order.name, ''
assert_not_equal @order.address, ''
assert_not_equal @order.email, ''
assert @order.valid?
end
end
Fixtures:
# orders.yml
one:
name: Dave Thomas
address: MyText
email: [email protected]
payment_type: check
two:
name: MyString
address: MyText
email: MyString
payment_type: check
#payment_types.yml
check:
name: Pay Check
# column: value
#
creditcard:
name: Credit Card
# column: value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment