Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

7.3 Playtime 7.1 Iteration B1: Validation and Unit Testing

7.2 Iteration B2: Unit Testing

5 tests, 23 assertions, 0 failures, 0 errors.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:69:in `block in <class:DepotTest>'

Introduce the importance of unit testing.

Look at what files are generated

ls test/unit
helpers
product_test.rb

Add some unit tests for new function.

edit test/unit/product_test.rb
require 'test_helper'
 
class ProductTest < ActiveSupport::TestCase
  test "product attributes must not be empty" do
    product = Product.new
    assert product.invalid?
    assert product.errors[:title].any?
    assert product.errors[:description].any?
    assert product.errors[:price].any?
    assert product.errors[:image_url].any?
  end
 
  test "product price must be positive" do
    product = Product.new(title:       "My Book Title",
                          description: "yyy",
                          image_url:   "zzz.jpg")
    product.price = -1
    assert product.invalid?
    assert_equal "must be greater than or equal to 0.01", 
      product.errors[:price].join('; ')
 
    product.price = 0
    assert product.invalid?
    assert_equal "must be greater than or equal to 0.01", 
      product.errors[:price].join('; ')
 
    product.price = 1
    assert product.valid?
  end
 
  def new_product(image_url)
    Product.new(title:       "My Book Title",
                description: "yyy",
                price:       1,
                image_url:   image_url)
  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|
      assert new_product(name).valid?, "#{name} shouldn't be invalid"
    end
 
    bad.each do |name|
      assert new_product(name).invalid?, "#{name} shouldn't be valid"
    end
  end
 
  test "product is not valid without a unique title" do
    product = Product.new(title:       products(:ruby).title,
                          description: "yyy", 
                          price:       1, 
                          image_url:   "fred.gif")
 
    assert !product.save
    assert_equal "has already been taken", product.errors[:title].join('; ')
  end
 
  test "product is not valid without a unique title - i18n" do
    product = Product.new(title:       products(:ruby).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

Look at existing test data

edit test/fixtures/products.yml
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
 
one:
  title: MyString
  description: MyText
  image_url: MyString
  price: 9.99
 
two:
  title: MyString
  description: MyText
  image_url: MyString
  price: 9.99

Add a fixture.

edit test/fixtures/products.yml
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
 
one:
  title: MyString
  description: MyText
  image_url: MyString
  price: 9.99
 
two:
  title: MyString
  description: MyText
  image_url: MyString
  price: 9.99
 
ruby: 
  title:       Programming Ruby 1.9
  description: 
    Ruby is the fastest growing and most exciting dynamic
    language out there.  If you need to get working programs
    delivered fast, you should add Ruby to your toolbox.
  price:       49.50
  image_url:   ruby.png 

Tests pass!

rake test:units
/home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: test/unit/**/*_test.rb (ArgumentError)
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:21:in `run'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:606:in `run'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
	from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
rake aborted!
Command failed with status (1): [/home/rubys/.rvm/rubies/ruby-1.9.3-r33476/...]

    
Tasks: TOP => test:units
(See full trace by running task with --trace)

7.3 Playtime 7.1 Iteration B1: Validation and Unit Testing