Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

T: Purchase and Item model coverage

I am not entirely proud of these tests...
  • Loading branch information...
commit 2f4e0d37862e88b05c58f8f255461ac1c59e2ebc 1 parent 42baec0
@rrix rrix authored
View
2  app/models/item.rb
@@ -1,4 +1,6 @@
class Item < ActiveRecord::Base
has_many :purchases
has_many :users, through: :purchases
+
+ validates_numericality_of :price, :greater_than_or_equal_to => 0.01
end
View
8 app/models/purchase.rb
@@ -2,9 +2,17 @@ class Purchase < ActiveRecord::Base
belongs_to :user
belongs_to :item
+ validates :price, :presence => true
+ validates :item, :presence => true
+ validates :user, :presence => true
+
attr_accessor :stripe_card_token
def save_with_payment
+ if !user or !item
+ return false
+ end
+
charge = Stripe::Charge.create amount: (item.price * 100).to_i,
card: stripe_card_token,
currency: 'usd',
View
2  spec/factories.rb
@@ -19,7 +19,7 @@
factory :item do
sequence(:name) { |n| "Item #{ n }" }
- sequence(:price) { |n| (n - 1) * 2.25 }
+ sequence(:price) { |n| (n) * 2.25 }
end
end
View
9 spec/models/item_spec.rb
@@ -1,5 +1,12 @@
require 'spec_helper'
describe Item do
- pending "add some examples to (or delete) #{__FILE__}"
+
+ it 'requires a price' do
+ Factory.build(:item, price: nil ).should_not be_valid
+ Factory.build(:item, price: "bacon" ).should_not be_valid
+ Factory.build(:item, price: "100.00").should be_valid
+ Factory.build(:item ).should be_valid
+ end
+
end
View
42 spec/models/purchase_spec.rb
@@ -1,5 +1,45 @@
require 'spec_helper'
describe Purchase do
- pending "add some examples to (or delete) #{__FILE__}"
+ use_vcr_cassette 'model/purchase', record: :all
+
+ let(:user) { Factory :user }
+ let(:item) { Factory :item, price: "10.00" }
+
+ before do
+ card = valid_card_data
+ charge = Stripe::Charge.create amount: (item.price * 100).to_i,
+ card: card,
+ currency: 'usd',
+ description: "#{ user.email } purchased #{ item.name }"
+ Stripe::Charge.stub!(:create).and_return(charge)
+ end
+
+ it 'should require a price' do
+ p = user.purchases.new(item: item)
+ p.save_with_payment
+ p.should be_valid
+
+ p = user.purchases.new(item: item)
+ p.save_with_payment
+ p.price = nil
+ p.should_not be_valid
+ end
+
+ it 'should require an item' do
+ p = user.purchases.new
+ p.save_with_payment
+ p.should_not be_valid
+ end
+
+ it 'should require a user' do
+ p = Purchase.new(item: item)
+ p.save_with_payment
+ p.should_not be_valid
+
+ p = Purchase.new(item: item, user: user)
+ p.save_with_payment
+ p.should be_valid
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.