7.2 Iteration B2: Unit Testing 6.3 Playtime
(1|0) tests, (1|0) assertions, 0 failures, 0 errors. Traceback: /home/rubys/git/awdwr/edition4/checkdepot.rb:62:in `block in <class:DepotTest>'
Augment the model with a few vailidity checks.
Various validations: required, numeric, positive, and unique
edit app/models/product.rb
class Product < ActiveRecord::Base
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)$}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
end
Demonstrate failures.
get /products/new
post /products
Demonstrate more failures.
get /products/new
post /products
edit app/models/product.rb
class Product < ActiveRecord::Base
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)$}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
end
Now run the tests... and watch them fail :-(
rake test
/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'
/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/functional/**/*_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'
Errors running test:units, test:functionals!
Solution is simple, provide valid data.
edit test/functional/products_controller_test.rb
require 'test_helper'
class ProductsControllerTest < ActionController::TestCase
setup do
@product = products(:one)
@update = {
title: 'Lorem Ipsum',
description: 'Wibbles are fun!',
image_url: 'lorem.jpg',
price: 19.95
}
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:products)
end
test "should get new" do
get :new
assert_response :success
end
test "should create product" do
assert_difference('Product.count') do
post :create, product: @update
end
assert_redirected_to product_path(assigns(:product))
end
# ...
test "should update product" do
put :update, id: @product.to_param, product: @update
assert_redirected_to product_path(assigns(:product))
end
# ...
end
Tests now pass again :-)
rake test
/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'
/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/functional/**/*_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'
Errors running test:units, test:functionals!