require 'test_helper' module RubyPsigate class ItemTest < Test::Unit::TestCase def setup @item = Item.new end %w( unique_id desc qty price ).each do |field| should "respond to #{field}" do assert @item.respond_to?(field.downcase.to_sym) setter = field.downcase + "=" assert @item.respond_to?(setter.to_sym) end end should "raise error if qty is less than 0" do assert_raises(NumberLessThanZero) do @item.qty = -3 end end should "not raise error if qty is 0" do assert_nothing_raised do @item.qty = 0 end end should "not raise error if qty above 0" do assert_nothing_raised do @item.qty = 1 end end should "set a whole number for qty" do @item.qty = "1.33" assert_equal "1.33".to_i, @item.qty end should "respond to price getter/setter" do assert @item.respond_to?(:price) assert @item.respond_to?(:price=) end should "set the amount to 100.00" do @item.price = "100.00" assert_equal Money.new(100.00*100), @item.price end should "respond to options" do assert @item.respond_to?(:options) end should "add options via << method" do assert_equal 0, @item.options.count @item << ItemOption.new(:color => "pink") assert_equal [{ :color => "pink"}], @item.options end should "raise an error unless added option is a ItemOption class" do assert_raises(InvalidItemOption) do @item << { :color => "pink" } end end should "return a simple item hash" do @item.unique_id = "PSI-BOOK" @item.desc = "XML Interface Doc" @item.qty = 2 @item.price = 10.00 @expectation = { :ItemID => "PSI-BOOK", :ItemDescription => "XML Interface Doc", :ItemQty => 2, :ItemPrice => 10.00 } assert_equal @expectation, @item.to_hash end should "return a nested item hash (with options)" do @item.unique_id = "PSI-BOOK" @item.desc = "XML Interface Doc" @item.qty = 2 @item.price = 10.00 @item << ItemOption.new(:color => "pink") @item << ItemOption.new(:size => "Extra Large") @expectation = { :ItemID => "PSI-BOOK", :ItemDescription => "XML Interface Doc", :ItemQty => 2, :ItemPrice => 10.00, :Option => { :color => "pink", :size => "Extra Large" } } assert_equal @expectation, @item.to_hash end end end