File: customized_base_test.rb

package info (click to toggle)
ruby-inherited-resources 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 824 kB
  • sloc: ruby: 4,388; makefile: 6
file content (184 lines) | stat: -rw-r--r-- 4,986 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
require 'test_helper'

class Car
  extend ActiveModel::Naming
end

class CarsController < InheritedResources::Base
  respond_to :html

  protected

    def collection
      @cars ||= Car.get_all
    end

    def build_resource
      @car ||= Car.create_new(params[:car])
    end

    def resource
      @car ||= Car.get(params[:id])
    end

    def create_resource(resource)
      resource.save_successfully
    end

    def update_resource(resource, attributes)
      resource.update_successfully(*attributes)
    end

    def destroy_resource(resource)
      resource.destroy_successfully
    end
end

module CarTestHelper
  def setup
    draw_routes do
      resources :cars
    end

    @controller          = CarsController.new
    @controller.request  = @request  = new_request
    @controller.response = @response = new_response
    @controller.stubs(:car_url).returns("/")
  end

  def teardown
    clear_routes
  end

  protected

    def new_request
      ActionController::TestRequest.create(CarsController)
    end

    def new_response
      ActionDispatch::TestResponse.create
    end

    def mock_car(expectations={})
      @mock_car ||= begin
        car = mock(expectations.except(:errors))
        car.stubs(:class).returns(Car)
        car.stubs(:errors).returns(expectations.fetch(:errors, {}))
        car
      end
    end
end

class IndexActionCustomizedBaseTest < ActionController::TestCase
  include CarTestHelper

  def test_expose_all_users_as_instance_variable
    Car.expects(:get_all).returns([mock_car])
    get :index
    assert_equal [mock_car], assigns(:cars)
  end
end

class ShowActionCustomizedBaseTest < ActionController::TestCase
  include CarTestHelper

  def test_expose_the_requested_user
    Car.expects(:get).with('42').returns(mock_car)
    get :show, params: { id: '42' }
    assert_equal mock_car, assigns(:car)
  end
end

class NewActionCustomizedBaseTest < ActionController::TestCase
  include CarTestHelper

  def test_expose_a_new_user
    Car.expects(:create_new).returns(mock_car)
    get :new
    assert_equal mock_car, assigns(:car)
  end
end

class EditActionCustomizedBaseTest < ActionController::TestCase
  include CarTestHelper

  def test_expose_the_requested_user
    Car.expects(:get).with('42').returns(mock_car)
    get :edit, params: { id: '42' }
    assert_response :success
    assert_equal mock_car, assigns(:car)
  end
end

class CreateActionCustomizedBaseTest < ActionController::TestCase
  include CarTestHelper

  def test_expose_a_newly_create_user_when_saved_with_success
    Car.expects(:create_new).with({'these' => 'params'}).returns(mock_car(save_successfully: true))
    post :create, params: { car: {these: 'params'} }
    assert_equal mock_car, assigns(:car)
  end

  def test_redirect_to_the_created_user
    Car.stubs(:create_new).returns(mock_car(save_successfully: true))
    @controller.expects(:resource_url).returns('http://test.host/')
    post :create
    assert_redirected_to 'http://test.host/'
  end

  def test_render_new_template_when_user_cannot_be_saved
    Car.stubs(:create_new).returns(mock_car(save_successfully: false, errors: {some: :error}))
    post :create
    assert_response :success
    assert_equal "New HTML", @response.body.strip
  end
end

class UpdateActionCustomizedBaseTest < ActionController::TestCase
  include CarTestHelper

  def test_update_the_requested_object
    Car.expects(:get).with('42').returns(mock_car)
    mock_car.expects(:update_successfully).with({'these' => 'params'}).returns(true)
    put :update, params: { id: '42', car: {these: 'params'} }
    assert_equal mock_car, assigns(:car)
  end

  def test_redirect_to_the_created_user
    Car.stubs(:get).returns(mock_car(update_successfully: true))
    @controller.expects(:resource_url).returns('http://test.host/')
    put :update, params: { id: '42' }
    assert_redirected_to 'http://test.host/'
  end

  def test_render_edit_template_when_user_cannot_be_saved
    Car.stubs(:get).returns(mock_car(update_successfully: false, errors: {some: :error}))
    put :update, params: { id: '42' }
    assert_response :success
    assert_equal "Edit HTML", @response.body.strip
  end
end

class DestroyActionCustomizedBaseTest < ActionController::TestCase
  include CarTestHelper

  def test_the_requested_user_is_destroyed
    Car.expects(:get).with('42').returns(mock_car)
    mock_car.expects(:destroy_successfully)
    delete :destroy, params: { id: '42' }
    assert_equal mock_car, assigns(:car)
  end

  def test_show_flash_message_when_user_can_be_deleted
    Car.stubs(:get).returns(mock_car(destroy_successfully: true))
    delete :destroy, params: { id: '42' }
    assert_equal flash[:notice], 'Car was successfully destroyed.'
  end

  def test_show_flash_message_when_cannot_be_deleted
    Car.stubs(:get).returns(mock_car(destroy_successfully: false, errors: { fail: true }))
    delete :destroy, params: { id: '42' }
    assert_equal flash[:alert], 'Car could not be destroyed.'
  end
end