7.2 Iteration B2: Unit Testing 6.3 Playtime
RuntimeError: Edit test/controllers/products_controller_test.rb failed at makedepot.rb:408 Traceback:
Augment the model with a few vailidity checks.
Various validations: required, numeric, positive, and unique
edit app/models/product.rb
class Product < ApplicationRecord
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 < ApplicationRecord
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 :-(
rails 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 included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
Run options: --seed 43638
# 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-2.3.0/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-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks.rb:15)
.F
Failure:
ProductsControllerTest#test_should_create_product:
"Product.count" didn't change by 1.
Expected: 3
Actual: 2
bin/rails test test/controllers/products_controller_test.rb:19
....F
Failure:
ProductsControllerTest#test_should_update_product:
Expected response to be a <3XX: redirect>, but was a <200: OK>
bin/rails test test/controllers/products_controller_test.rb:38
Finished in 0.246315s, 28.4189 runs/s, 36.5386 assertions/s.
7 runs, 9 assertions, 2 failures, 0 errors, 0 skips
Solution is simple, provide valid data.
edit test/controllers/products_controller_test.rb
#<IndexError: regexp not matched> /home/rubys/git/gorp/lib/gorp/edit.rb:91:in `edit' makedepot.rb:408:in `block (4 levels) in <main>' /home/rubys/git/gorp/lib/gorp/edit.rb:111:in `instance_exec' /home/rubys/git/gorp/lib/gorp/edit.rb:111:in `block in dcl' /home/rubys/git/gorp/lib/gorp/edit.rb:109:in `sub!' /home/rubys/git/gorp/lib/gorp/edit.rb:109:in `dcl' makedepot.rb:402:in `block (3 levels) in <main>' makedepot.rb:401:in `each' makedepot.rb:401:in `block (2 levels) in <main>' /home/rubys/git/gorp/lib/gorp/edit.rb:173:in `instance_exec' /home/rubys/git/gorp/lib/gorp/edit.rb:173:in `edit' makedepot.rb:383:in `block in <main>' /home/rubys/git/gorp/lib/gorp/output.rb:59:in `block (4 levels) in <top (required)>' /home/rubys/git/gorp/lib/gorp/output.rb:49:in `each' /home/rubys/git/gorp/lib/gorp/output.rb:49:in `block (3 levels) in <top (required)>' /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:175:in `_nested_structures' /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:68:in `tag!' /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:93:in `method_missing' /home/rubys/git/gorp/lib/gorp/output.rb:22:in `block (2 levels) in <top (required)>' /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:175:in `_nested_structures' /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:68:in `tag!' /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:93:in `method_missing' /home/rubys/git/gorp/lib/gorp/output.rb:11:in `block in <top (required)>'
require 'test_helper'
class ProductsControllerTest < ActionDispatch::IntegrationTest
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 products_url
assert_response :success
end
test "should get new" do
get new_product_url
assert_response :success
end
test "should create product" do
assert_difference('Product.count') do
post products_url, params: { product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title } }
end
assert_redirected_to product_path(Product.last)
end
test "should show product" do
get product_url(@product)
assert_response :success
end
test "should get edit" do
get edit_product_url(@product)
assert_response :success
end
test "should update product" do
patch product_url(@product), params: { product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title } }
assert_redirected_to product_path(@product)
end
test "should destroy product" do
assert_difference('Product.count', -1) do
delete product_url(@product)
end
assert_redirected_to products_path
end
end
Tests now pass again :-)
rails 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 included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
Run options: --seed 37196
# 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-2.3.0/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-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks.rb:15)
.F
Failure:
ProductsControllerTest#test_should_create_product:
"Product.count" didn't change by 1.
Expected: 3
Actual: 2
bin/rails test test/controllers/products_controller_test.rb:19
...F
Failure:
ProductsControllerTest#test_should_update_product:
Expected response to be a <3XX: redirect>, but was a <200: OK>
bin/rails test test/controllers/products_controller_test.rb:38
.
Finished in 0.238259s, 29.3797 runs/s, 37.7740 assertions/s.
7 runs, 9 assertions, 2 failures, 0 errors, 0 skips