Sha256: a7d0e991de40d5847a7e8e49d2639f59295b7fc6b2e9a0053936208ec623d8ee

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

module SimpleCart
  RSpec.describe Cart, type: :model do
    let(:thing1) { FactoryGirl.create(:thing) }
    let(:thing2) { FactoryGirl.create(:thing) }
    let(:cart) { SimpleCart::Cart.new("#{thing1.class.to_s}_#{thing1.id}" => 5, "#{thing2.class.to_s}_#{thing2.id}" => 2) }

    it "correct stubbed models" do
      expect(thing1).to be_a(Thing)
      expect(thing2).to be_a(Thing)
    end

    it '#subtotal' do
      expect(cart.subtotal).to eq(thing1.price * 5 + thing2.price * 2)
    end

    it "#items" do
      expect(cart.items).to eq([thing1, thing2])
    end

    it "#order_items" do
      expect(cart.order_items).to be_a(Array)
      expect(cart.order_items).to include(a_kind_of(OrderItem))
    end

    it "#build_order" do
      expect(cart.build_order).to be_a(Order)
    end

    it '#add_item' do
      expect { cart.add_item(12, 2) }.to change { cart.session }
    end

    it '#remove_item' do 
      expect { cart.remove_item("#{thing1.class.to_s}_#{thing1.id}") }.to change {cart.session}.from({"#{thing1.class.to_s}_#{thing1.id}"=>5, "#{thing2.class.to_s}_#{thing2.id}" =>2}).to({"#{thing2.class.to_s}_#{thing2.id}"=>2})
    end

    it '#update_items' do
      params = {"#{thing1.class.to_s}_#{thing1.id}" => 2, "#{thing2.class.to_s}_#{thing2.id}" => 1}
      expect { cart.update_items(params) }.to change{cart.session}.from({"#{thing1.class.to_s}_#{thing1.id}"=>5, "#{thing2.class.to_s}_#{thing2.id}"=>2}).to({"#{thing1.class.to_s}_#{thing1.id}"=>2, "#{thing2.class.to_s}_#{thing2.id}"=>1})
    end

    # it '#discount' do
    #   coupon = create(:coupon)
    #   expect(cart.discount(coupon.code)).to eq(cart.subtotal*coupon.discount/100.0)
    # end
  end 
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_cart-0.0.5 spec/models/simple_cart/cart_spec.rb
simple_cart-0.0.4 spec/models/simple_cart/cart_spec.rb