7.2 Iteration B2: Unit Testing 6.3 Playtime
7 (tests|runs), 13 assertions, 0 failures, 0 errors. <0> expected to be >= <1>. Traceback: /home/rubys/git/awdwr/edition4/checkdepot.rb:36:in `assert_test_summary' /home/rubys/git/awdwr/edition4/checkdepot.rb:100: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)\Z}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)\Z}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
end
Now run the tests... and watch them fail :-(
rake test
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from block in <top (required)> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/extensions.rb:18)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
Run options: --seed 27826
# Running:
DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead. (called from block (3 levels) in <class:Engine> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks.rb:14)
DEPRECATION WARNING: after_filter is deprecated and will be removed in Rails 5.1. Use after_action instead. (called from block (3 levels) in <class:Engine> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks.rb:15)
..F...F
Finished in 0.151665s, 46.1543 runs/s, 65.9347 assertions/s.
1) Failure:
ProductsControllerTest#test_should_create_product [/home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/products_controller_test.rb:20]:
"Product.count" didn't change by 1.
Expected: 3
Actual: 2
2) Failure:
ProductsControllerTest#test_should_update_product [/home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/products_controller_test.rb:39]:
Expected response to be a <redirect>, but was <200>
7 runs, 10 assertions, 2 failures, 0 errors, 0 skips
Failed tests:
bin/rails test /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/products_controller_test.rb:19
bin/rails test /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/products_controller_test.rb:37
Solution is simple, provide valid data.
edit test/controllers/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, params: @update
end
assert_redirected_to product_path(assigns(:product))
end
# ...
test "should update product" do
patch :update, params: @update
assert_redirected_to product_path(assigns(:product))
end
# ...
end
Tests now pass again :-)
rake test
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from block in <top (required)> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/extensions.rb:18)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
Run options: --seed 64136
# Running:
DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead. (called from block (3 levels) in <class:Engine> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks.rb:14)
DEPRECATION WARNING: after_filter is deprecated and will be removed in Rails 5.1. Use after_action instead. (called from block (3 levels) in <class:Engine> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks.rb:15)
E..E...
Finished in 0.121320s, 57.6986 runs/s, 65.9413 assertions/s.
1) Error:
ProductsControllerTest#test_should_create_product:
ActionController::ParameterMissing: param is missing or the value is empty: product
app/controllers/products_controller.rb:79:in `product_params'
app/controllers/products_controller.rb:27:in `create'
test/controllers/products_controller_test.rb:34:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:32:in `block in <class:ProductsControllerTest>'
2) Error:
ProductsControllerTest#test_should_update_product:
ActionController::UrlGenerationError: No route matches {:action=>"update", :controller=>"products", :description=>"Wibbles are fun!", :image_url=>"lorem.jpg", :price=>"19.95", :title=>"Lorem Ipsum"}
test/controllers/products_controller_test.rb:57:in `block in <class:ProductsControllerTest>'
7 runs, 8 assertions, 0 failures, 2 errors, 0 skips
Failed tests:
bin/rails test /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/products_controller_test.rb:31
bin/rails test /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/products_controller_test.rb:55