1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
require 'test_helper'
class Brands; end
class Category; end
class Product
def self.human_name; 'Product'; end
end
class ProductsController < InheritedResources::Base
belongs_to :brand, :category, polymorphic: true, optional: true
end
class OptionalTest < ActionController::TestCase
tests ProductsController
def setup
draw_routes do
resources :products
end
@controller.stubs(:resource_url).returns('/')
end
def teardown
clear_routes
end
def test_expose_all_products_as_instance_variable_with_category
Category.expects(:find).with('37').returns(mock_category)
mock_category.expects(:products).returns(Product)
Product.expects(:scoped).returns([mock_product])
get :index, params: { category_id: '37' }
assert_equal mock_category, assigns(:category)
assert_equal [mock_product], assigns(:products)
end
def test_expose_all_products_as_instance_variable_without_category
Product.expects(:scoped).returns([mock_product])
get :index
assert_nil assigns(:category)
assert_equal [mock_product], assigns(:products)
end
def test_expose_the_requested_product_with_category
Category.expects(:find).with('37').returns(mock_category)
mock_category.expects(:products).returns(Product)
Product.expects(:find).with('42').returns(mock_product)
get :show, params: { id: '42', category_id: '37' }
assert_equal mock_category, assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_expose_the_requested_product_without_category
Product.expects(:find).with('42').returns(mock_product)
get :show, params: { id: '42' }
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_expose_a_new_product_with_category
Category.expects(:find).with('37').returns(mock_category)
mock_category.expects(:products).returns(Product)
Product.expects(:build).returns(mock_product)
get :new, params: { category_id: '37' }
assert_equal mock_category, assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_expose_a_new_product_without_category
Product.expects(:new).returns(mock_product)
get :new
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_expose_the_requested_product_for_edition_with_category
Category.expects(:find).with('37').returns(mock_category)
mock_category.expects(:products).returns(Product)
Product.expects(:find).with('42').returns(mock_product)
get :edit, params: { id: '42', category_id: '37' }
assert_equal mock_category, assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_expose_the_requested_product_for_edition_without_category
Product.expects(:find).with('42').returns(mock_product)
get :edit, params: { id: '42' }
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_expose_a_newly_create_product_with_category
Category.expects(:find).with('37').returns(mock_category)
mock_category.expects(:products).returns(Product)
Product.expects(:build).with({'these' => 'params'}).returns(mock_product(save: true))
post :create, params: { category_id: '37', product: {these: 'params'} }
assert_equal mock_category, assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_expose_a_newly_create_product_without_category
Product.expects(:new).with({'these' => 'params'}).returns(mock_product(save: true))
post :create, params: { product: {these: 'params'} }
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_update_the_requested_object_with_category
Category.expects(:find).with('37').returns(mock_category)
mock_category.expects(:products).returns(Product)
Product.expects(:find).with('42').returns(mock_product)
mock_product.expects(:update).with({'these' => 'params'}).returns(true)
put :update, params: { id: '42', category_id: '37', product: {these: 'params'} }
assert_equal mock_category, assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_update_the_requested_object_without_category
Product.expects(:find).with('42').returns(mock_product)
mock_product.expects(:update).with({'these' => 'params'}).returns(true)
put :update, params: { id: '42', product: {these: 'params'} }
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_the_requested_product_is_destroyed_with_category
Category.expects(:find).with('37').returns(mock_category)
mock_category.expects(:products).returns(Product)
Product.expects(:find).with('42').returns(mock_product)
mock_product.expects(:destroy).returns(true)
@controller.expects(:collection_url).returns('/')
delete :destroy, params: { id: '42', category_id: '37' }
assert_equal mock_category, assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_the_requested_product_is_destroyed_without_category
Product.expects(:find).with('42').returns(mock_product)
mock_product.expects(:destroy).returns(true)
@controller.expects(:collection_url).returns('/')
delete :destroy, params: { id: '42' }
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end
def test_polymorphic_helpers
Product.expects(:scoped).returns([mock_product])
get :index
assert !@controller.send(:parent?)
assert_nil assigns(:parent_type)
assert_nil @controller.send(:parent_type)
assert_nil @controller.send(:parent_class)
assert_nil assigns(:category)
assert_nil @controller.send(:parent)
end
protected
def mock_category(stubs={})
@mock_category ||= mock(stubs)
end
def mock_product(stubs={})
@mock_product ||= mock(stubs)
end
end
|