# == Schema Information # # Table name: coupons # # id :integer(4) not null, primary key # code :string(255) # amount :integer(4) default(0), not null # percent :integer(4) default(0), not null # uses :integer(4) default(0), not null # unlimited :boolean(1) not null # expires_at :datetime # use_limit :integer(4) default(1), not null # created_at :datetime # updated_at :datetime # use_for_times :integer(4) # use_for_months :integer(4) # user_id :integer(4) # coupon_type_id :integer(4) # require File.dirname(__FILE__) + '/../test_helper' class CouponTest < ActiveSupport::TestCase context 'A coupon instance that acts_as_muck_coupon' do setup do @coupon = Factory(:coupon) end subject { @coupon } should_have_many :orders should_belong_to :user should_belong_to :coupon_type should_validate_presence_of :code, :amount, :percent, :uses should_validate_uniqueness_of :code #validate :validate_discount context "named scopes" do setup do @active_coupon = Factory(:coupon) @expired_coupon = Factory(:coupon, :expires_at => 1.week.ago) @used_coupon = Factory(:coupon, :use_limit => 1, :uses => 2) @referral_coupon = Factory(:coupon, :coupon_type => CouponType.default) end should_scope_by_newest context "active" do should "find active coupons" do assert Coupon.active.include?(@active_coupon) end should "not find expired coupons" do assert !Coupon.active.include?(@in_active_coupon) end should "not find used coupons" do assert !Coupon.active.include?(@used_coupon) end end context "referrals" do should "find referral codes" do assert Coupon.referrals.include?(@referral_coupon) end should "not find default codes" do assert !Coupon.active.include?(@active_coupon) end end context "default" do should "find default codes" do assert Coupon.referrals.include?(@active_coupon) end should "not find referral code" do assert !Coupon.active.include?(@referral_coupon) end end end context "random_code" do should "generate a random code" do assert_not_nil Coupon.random_code end end end end