Skip to content

Instantly share code, notes, and snippets.

@radar
Forked from NakedMoleRatScientist/gist:749128
Created December 20, 2010 22:40
Show Gist options
  • Select an option

  • Save radar/749140 to your computer and use it in GitHub Desktop.

Select an option

Save radar/749140 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Bid do
before(:each) do
@user = Factory(:user)
@user.funds = 10
@user.save()
@prediction = Factory(:prediction)
@attr= {
:user_id => @user.id,
:prediction_id => @prediction.id,
:txn => 10,
:bet => true
}
end
it "should create a new instance given valid attributes" do
@user.bids.create(@attr)
@user.save()
@user.bids.size.should == 1
@user.funds.should == 0
end
it "should not create a new instance if funds is below txn" do
@user.funds = 9
@user.save()
@user.bids.create(@attr)
@user.save()
@user.bids.size.should == 0
@user.funds.should == 1
end
it "should not create a new instance if prediction id is empty" do
@user.bids.create(@attr.merge(:prediction_id => nil))
@user.save()
@user.bids.size.should == 0
end
end
class Bid < ActiveRecord::Base
belongs_to :user
validate :check_fund
validates_presence_of :prediction_id
def check_fund
errors.add(:user, "'s funds is insufficent") unless user.funds >= self.txn
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment