products_controller_test.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. require 'test_helper'
  18. class ProductsControllerTest < ActionController::TestCase
  19. setup do
  20. @product = products(:one)
  21. @update = {
  22. :title => 'Lorem Ipsum',
  23. :description => 'Wibbles are fun!',
  24. :image_url => 'lorem.jpg',
  25. :price => 19.95
  26. }
  27. end
  28. test "should get index" do
  29. get :index
  30. assert_response :success
  31. assert_not_nil assigns(:products)
  32. end
  33. test "should get new" do
  34. get :new
  35. assert_response :success
  36. end
  37. test "should create product" do
  38. assert_difference('Product.count') do
  39. post :create, :product => @update
  40. end
  41. assert_redirected_to product_path(assigns(:product))
  42. end
  43. test "should show product" do
  44. get :show, :id => @product.to_param
  45. assert_response :success
  46. end
  47. test "should get edit" do
  48. get :edit, :id => @product.to_param
  49. assert_response :success
  50. end
  51. test "should update product" do
  52. put :update, :id => @product.to_param, :product => @update
  53. assert_redirected_to product_path(assigns(:product))
  54. end
  55. test "can't delete product in cart" do
  56. assert_difference('Product.count', 0) do
  57. delete :destroy, :id => products(:ruby).to_param
  58. end
  59. assert_redirected_to products_path
  60. end
  61. test "should destroy product" do
  62. assert_difference('Product.count', -1) do
  63. delete :destroy, :id => @product.to_param
  64. end
  65. assert_redirected_to products_path
  66. end
  67. end