products_controller_test.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. require 'test_helper'
  10. class ProductsControllerTest < ActionController::TestCase
  11. setup do
  12. @product = products(:one)
  13. @update = {
  14. title: 'Lorem Ipsum',
  15. description: 'Wibbles are fun!',
  16. image_url: 'lorem.jpg',
  17. price: 19.95
  18. }
  19. end
  20. test "should get index" do
  21. get :index
  22. assert_response :success
  23. assert_not_nil assigns(:products)
  24. end
  25. test "should get new" do
  26. get :new
  27. assert_response :success
  28. end
  29. test "should create product" do
  30. assert_difference('Product.count') do
  31. post :create, product: @update
  32. end
  33. assert_redirected_to product_path(assigns(:product))
  34. end
  35. # ...
  36. test "should show product" do
  37. get :show, id: @product.to_param
  38. assert_response :success
  39. end
  40. test "should get edit" do
  41. get :edit, id: @product.to_param
  42. assert_response :success
  43. end
  44. test "should update product" do
  45. put :update, id: @product.to_param, product: @update
  46. assert_redirected_to product_path(assigns(:product))
  47. end
  48. # ...
  49. test "should destroy product" do
  50. assert_difference('Product.count', -1) do
  51. delete :destroy, id: @product.to_param
  52. end
  53. assert_redirected_to products_path
  54. end
  55. end