The Depot Application

The Depot Application

14.3 Functional Testing of Controllers 14.1 Tests Baked Right In

14.2 Unit Testing of Models

cat test/unit/product_test.rb
require 'test_helper'
 
class ProductTest < ActiveSupport::TestCase
  # Replace this with your real tests.
  test "the truth" do
    assert true
  end
end
ruby -Itest test/unit/product_test.rb
Loaded suite test/unit/product_test
Started
E
 
  1) Error:
test_the_truth(ProductTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: line_items: DELETE FROM "line_items" WHERE 1=1
 
 
Finished in 0.700208198 seconds.
 
1 tests, 0 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
rake db:test:prepare
(in /home/rubys/git/awdwr/work-191-239/depot)
ruby -Itest test/unit/product_test.rb
Loaded suite test/unit/product_test
Started
.
 
Finished in 0.823295936 seconds.
 
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
rake test:units
/home/rubys/.rvm/rubies/ruby-1.9.1-p378/bin/ruby -I"lib:test" "/home/rubys/.rvm/gems/ruby-1.9.1-p378@global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/helpers/products_helper_test.rb" "test/unit/helpers/info_helper_test.rb" "test/unit/helpers/admin_helper_test.rb" "test/unit/helpers/line_items_helper_test.rb" "test/unit/helpers/orders_helper_test.rb" "test/unit/helpers/users_helper_test.rb" "test/unit/helpers/store_helper_test.rb" "test/unit/product_test.rb" "test/unit/user_test.rb" "test/unit/line_item_test.rb" "test/unit/order_test.rb" 
(in /home/rubys/git/awdwr/work-191-239/depot)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.1-p378@global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
....
 
Finished in 5.067686082 seconds.
 
4 tests, 4 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
edit test/unit/product_test.rb
require 'test_helper'
 
class ProductTest < ActiveSupport::TestCase
  
  fixtures :products
  
  # Replace this with your real tests.
  test "the truth" do
    assert true
  end
 
  test "invalid with empty attributes" do
    product = Product.new
    assert !product.valid?
    assert product.errors.invalid?(:title)
    assert product.errors.invalid?(:description)
    assert product.errors.invalid?(:price)
    assert product.errors.invalid?(:image_url)
  end
 
  test "positive price" do
    product = Product.new(:title       => "My Book Title",
                          :description => "yyy",
                          :image_url   => "zzz.jpg")
    product.price = -1
    assert !product.valid?
    assert_equal "should be at least 0.01", product.errors.on(:price)
 
    product.price = 0
    assert !product.valid?
    assert_equal "should be at least 0.01", product.errors.on(:price)
 
    product.price = 1
    assert product.valid?
  end
 
  test "image url" do
    ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg
             http://a.b.c/x/y/z/fred.gif }
    bad = %w{ fred.doc fred.gif/more fred.gif.more }
    
    ok.each do |name|
      product = Product.new(:title       => "My Book Title",
                            :description => "yyy",
                            :price       => 1,
                            :image_url   => name)
      assert product.valid?, product.errors.full_messages.join
    end
 
    bad.each do |name|
      product = Product.new(:title => "My Book Title",
                            :description => "yyy",
                            :price => 1,
                            :image_url => name)
      assert !product.valid?, "saving #{name}"
    end
  end
 
  test "unique title" do
    product = Product.new(:title       => products(:ruby_book).title,
                          :description => "yyy", 
                          :price       => 1, 
                          :image_url   => "fred.gif")
 
    assert !product.save
    assert_equal "has already been taken", product.errors.on(:title)
  end
 
  test "unique title1" do
    product = Product.new(:title       => products(:ruby_book).title,
                          :description => "yyy", 
                          :price       => 1, 
                          :image_url   => "fred.gif")
 
    assert !product.save
    assert_equal I18n.translate('activerecord.errors.messages.taken'),
                 product.errors.on(:title)
  end
  
end
edit test/fixtures/products.yml
ruby_book:
  title:       Programming Ruby
  description: Dummy description
  price:       1234
  image_url:   ruby.png
 
rails_book:
  title:       Agile Web Development with Rails
  description: Dummy description
  price:       2345
  image_url:   rails.png
  
rake test:units
/home/rubys/.rvm/rubies/ruby-1.9.1-p378/bin/ruby -I"lib:test" "/home/rubys/.rvm/gems/ruby-1.9.1-p378@global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/helpers/products_helper_test.rb" "test/unit/helpers/info_helper_test.rb" "test/unit/helpers/admin_helper_test.rb" "test/unit/helpers/line_items_helper_test.rb" "test/unit/helpers/orders_helper_test.rb" "test/unit/helpers/users_helper_test.rb" "test/unit/helpers/store_helper_test.rb" "test/unit/product_test.rb" "test/unit/user_test.rb" "test/unit/line_item_test.rb" "test/unit/order_test.rb" 
(in /home/rubys/git/awdwr/work-191-239/depot)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.1-p378@global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.........
 
Finished in 6.326083972 seconds.
 
9 tests, 27 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
edit test/unit/cart_test.rb
require 'test_helper'
 
class CartTest < ActiveSupport::TestCase
  fixtures :products
  test "add unique products" do
    cart = Cart.new
    rails_book = products(:rails_book)
    ruby_book  = products(:ruby_book)
    cart.add_product rails_book
    cart.add_product ruby_book
    assert_equal 2, cart.items.size
    assert_equal rails_book.price + ruby_book.price, cart.total_price
  end
  
  test "add_duplicate_product" do
    cart = Cart.new
    rails_book = products(:rails_book)
    cart.add_product rails_book
    cart.add_product rails_book
    assert_equal 2*rails_book.price, cart.total_price
    assert_equal 1, cart.items.size
    assert_equal 2, cart.items[0].quantity
  end 
end
ruby -I test test/unit/cart_test.rb
Loaded suite test/unit/cart_test
Started
..
 
Finished in 0.495065825 seconds.
 
2 tests, 5 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
edit test/unit/cart_test1.rb
require 'test_helper'
 
class CartTest < ActiveSupport::TestCase
  fixtures :products
 
  def setup
    @cart  = Cart.new
    @rails = products(:rails_book)
    @ruby  = products(:ruby_book)
  end
  
  test "add unique products" do
    @cart.add_product @rails
    @cart.add_product @ruby
    assert_equal 2, @cart.items.size
    assert_equal @rails.price + @ruby.price, @cart.total_price
  end
 
  test "add duplicate product" do
    @cart.add_product @rails
    @cart.add_product @rails
    assert_equal 2*@rails.price, @cart.total_price
    assert_equal 1, @cart.items.size
    assert_equal 2, @cart.items[0].quantity
  end 
end
ruby -I test test/unit/cart_test1.rb
Loaded suite test/unit/cart_test1
Started
..
 
Finished in 0.591995265 seconds.
 
2 tests, 5 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

14.3 Functional Testing of Controllers 14.1 Tests Baked Right In