products_controller_test.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. end
  14. test "should get index" do
  15. get :index
  16. assert_response :success
  17. assert_not_nil assigns(:products)
  18. end
  19. test "should get new" do
  20. get :new
  21. assert_response :success
  22. end
  23. test "should create product" do
  24. assert_difference('Product.count') do
  25. post :create, product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title }
  26. end
  27. assert_redirected_to product_path(assigns(:product))
  28. end
  29. test "should show product" do
  30. get :show, id: @product
  31. assert_response :success
  32. end
  33. test "should get edit" do
  34. get :edit, id: @product
  35. assert_response :success
  36. end
  37. test "should update product" do
  38. put :update, id: @product, product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title }
  39. assert_redirected_to product_path(assigns(:product))
  40. end
  41. test "should destroy product" do
  42. assert_difference('Product.count', -1) do
  43. delete :destroy, id: @product
  44. end
  45. assert_redirected_to products_path
  46. end
  47. end