The Depot Application

The Depot Application

14.3 Functional Testing of Controllers 14.1 Tests Baked Right In

14.2 Unit Testing of Models

</4 tests, 4 assertions, 0 failures, 0 errors/> expected but was
<"require 'test_helper'">.

Traceback:
  /home/rubys/git/awdwr/checkdepot.rb:220:in `block in <class:DepotTest>'
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
/home/rubys/.rvm/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
Loaded suite test/unit/product_test
Started
E
Finished in 0.020580 seconds.
 
  1) Error:
test_the_truth(ProductTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: line_items: DELETE FROM "line_items" WHERE 1=1
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:194:in `log'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in `execute'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:269:in `update_sql'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:139:in `update_sql'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:274:in `delete_sql'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:145:in `delete_sql'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:54:in `delete'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:16:in `delete'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:561:in `delete_existing_fixtures'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:523:in `block (4 levels) in create_fixtures'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:523:in `each'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:523:in `block (3 levels) in create_fixtures'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:522:in `block (2 levels) in create_fixtures'
    /home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:104:in `disable_referential_integrity'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:513:in `block in create_fixtures'
    /home/rubys/git/rails/activesupport/lib/active_support/benchmarkable.rb:55:in `silence'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:512:in `create_fixtures'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:971:in `load_fixtures'
    /home/rubys/git/rails/activerecord/lib/active_record/fixtures.rb:936:in `setup_fixtures'
    /home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:409:in `_run_setup_callbacks'
    /home/rubys/git/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
 
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
 
Test run options: --seed 1082
rake db:test:prepare
/home/rubys/.rvm/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
(in /home/rubys/git/awdwr/work-193/depot)
ruby -Itest test/unit/product_test.rb
/home/rubys/.rvm/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
Loaded suite test/unit/product_test
Started
.
Finished in 0.053654 seconds.
 
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
 
Test run options: --seed 27655
rake test:units
/home/rubys/.rvm/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
(in /home/rubys/git/awdwr/work-193/depot)
/home/rubys/.rvm/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
/home/rubys/git/rails/activesupport/lib/active_support/testing/declarative.rb:28:in `test': test_the_truth is already defined in ProductTest (RuntimeError)
	from /home/rubys/git/awdwr/work-193/depot/test/unit/product_test.rb:5:in `<class:ProductTest>'
	from /home/rubys/git/awdwr/work-193/depot/test/unit/product_test.rb:3:in `<top (required)>'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:219:in `require'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:219:in `block in require'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:207:in `load_dependency'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:219:in `require'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:79:in `block in non_options'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:73:in `each'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:73:in `non_options'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:45:in `process_args'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/minitest/unit.rb:576:in `run'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:21:in `run'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:163:in `block (2 levels) in autorun'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:163:in `block in autorun'
rake aborted!
Command failed with status (1): [/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/...]
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:993:in `block in sh'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1008:in `call'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1008:in `sh'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1092:in `sh'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1027:in `ruby'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1092:in `ruby'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:25:in `block (2 levels) in define'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1110:in `verbose'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:10:in `block in define'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:634:in `call'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:634:in `block in execute'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:629:in `each'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:629:in `execute'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:595:in `block in invoke_with_call_chain'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:588:in `invoke_with_call_chain'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:581:in `invoke'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2041:in `invoke_task'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2019:in `block (2 levels) in top_level'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2019:in `each'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2019:in `block in top_level'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2013:in `top_level'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1992:in `run'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/bin/rake:36:in `<main>'
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[:title].any?
    assert product.errors[:description].any?
    assert product.errors[:price].any?
    assert product.errors[:image_url].any?
  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[:price].join
 
    product.price = 0
    assert !product.valid?
    assert_equal "should be at least 0.01", product.errors[:price].join
 
    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[:title].join
  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[:title].join
  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/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
(in /home/rubys/git/awdwr/work-193/depot)
/home/rubys/.rvm/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
/home/rubys/git/rails/activesupport/lib/active_support/testing/declarative.rb:28:in `test': test_the_truth is already defined in ProductTest (RuntimeError)
	from /home/rubys/git/awdwr/work-193/depot/test/unit/product_test.rb:13:in `<class:ProductTest>'
	from /home/rubys/git/awdwr/work-193/depot/test/unit/product_test.rb:4:in `<top (required)>'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:219:in `require'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:219:in `block in require'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:207:in `load_dependency'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:219:in `require'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:79:in `block in non_options'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:73:in `each'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:73:in `non_options'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:45:in `process_args'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/minitest/unit.rb:576:in `run'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:21:in `run'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:163:in `block (2 levels) in autorun'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/test/unit.rb:163:in `block in autorun'
rake aborted!
Command failed with status (1): [/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/...]
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:993:in `block in sh'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1008:in `call'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1008:in `sh'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1092:in `sh'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1027:in `ruby'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1092:in `ruby'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:25:in `block (2 levels) in define'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1110:in `verbose'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:10:in `block in define'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:634:in `call'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:634:in `block in execute'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:629:in `each'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:629:in `execute'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:595:in `block in invoke_with_call_chain'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:588:in `invoke_with_call_chain'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:581:in `invoke'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2041:in `invoke_task'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2019:in `block (2 levels) in top_level'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2019:in `each'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2019:in `block in top_level'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:2013:in `top_level'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/lib/ruby/1.9.1/rake.rb:1992:in `run'
/home/rubys/.rvm/rubies/ruby-1.9.3-r28929/bin/rake:36:in `<main>'
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
/home/rubys/.rvm/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
Loaded suite test/unit/cart_test
Started
..
Finished in 0.104533 seconds.
 
2 tests, 5 assertions, 0 failures, 0 errors, 0 skips
 
Test run options: --seed 28458
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
/home/rubys/.rvm/gems/ruby-1.9.3-r28929/gems/bundler-1.0.0.rc.3/lib/bundler.rb:183: Use RbConfig instead of obsolete and deprecated Config.
Loaded suite test/unit/cart_test1
Started
..
Finished in 0.106241 seconds.
 
2 tests, 5 assertions, 0 failures, 0 errors, 0 skips
 
Test run options: --seed 27904

14.3 Functional Testing of Controllers 14.1 Tests Baked Right In