From 383a554b249ffe7234608759f5b8dc9ab513918c Mon Sep 17 00:00:00 2001 From: "Butler, Alexis (UG - Computer Science)" <ab02259@surrey.ac.uk> Date: Sun, 29 Nov 2020 14:40:58 +0000 Subject: [PATCH] Wrote product contoller tests --- .../admin/products_controller_test.rb | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/test/controllers/admin/products_controller_test.rb b/test/controllers/admin/products_controller_test.rb index 71fd05a..5d890a3 100644 --- a/test/controllers/admin/products_controller_test.rb +++ b/test/controllers/admin/products_controller_test.rb @@ -17,21 +17,59 @@ class Admin::ProductsControllerTest < ActionDispatch::IntegrationTest assert_equal 'You must be admin to go there!', flash[:danger] end test 'should get new product form' do - + sign_in_as_admin + get new_admin_product_url + assert_response(:success) + assert_select 'form#NewProdForm' do + assert_select 'input', 6 + assert_select 'textarea', 1 + assert_select 'select', 1 + end end test 'should not get new prod form if not admin' do - + sign_in_as_tester + get new_admin_product_url + assert_response(:redirect) + assert_redirected_to(root_url) + assert_equal 'You must be admin to go there!', flash[:danger] end test 'should create a new product' do - + sign_in_as_admin + imgFile = fixture_file_upload('files/Tester.png') + assert_difference('Product.count',1) do + post admin_products_url, params: {product: {name: 'tsetdorp', description: 'test description', image: imgFile, + stockCount: 23, pricePence: 42, section_id: Section.first.id}, + commit: 'Create Product'} + end + assert_response(:redirect) + assert_redirected_to(admin_products_url) + assert_equal "New product created", flash[:success] end test 'should not create a new product if not admin' do - + sign_in_as_tester + post admin_products_url, params: {product: {name: 'tsetdorp', description: 'test description', image: "asd", + stockCount: 23, pricePence: 42, section_id: Section.first.id}, + commit: 'Create Product'} + assert_response(:redirect) + assert_redirected_to(root_url) + assert_equal 'You must be admin to go there!', flash[:danger] end test 'should delete a product' do - + sign_in_as_admin + assert_difference 'Product.count', -1 do + delete admin_product_url(products(:one)) + end + assert_response(:redirect) + assert_redirected_to(admin_products_url) + assert_equal 'Product deleted', flash[:success] end test 'should not delete a product if not admin' do - + sign_in_as_tester + assert_no_difference 'Product.count' do + delete admin_product_url(products(:one)) + end + assert_response(:redirect) + assert_redirected_to(root_url) + assert_equal 'You must be admin to go there!', flash[:danger] end end -- GitLab