File: aliases_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 (156 lines) | stat: -rw-r--r-- 4,472 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
require 'test_helper'

class Student
  extend ActiveModel::Naming
end

class ApplicationController < ActionController::Base
  include InheritedResources::DSL
end

class StudentsController < ApplicationController
  inherit_resources
  respond_to :html, :xml

  def edit
    edit! do |format|
      format.xml { render plain: 'Render XML' }
    end
  end

  def new
    @something = 'magical'
    new!
  end

  create!(location: "http://test.host/") do |success, failure|
    success.html { render plain: "I won't redirect!" }
    failure.xml { render plain: "I shouldn't be rendered" }
  end

  update! do |success, failure|
    success.html { redirect_to(resource_url) }
    failure.html { render plain: "I won't render!" }
  end

  destroy! do |format|
    format.html { render plain: "Destroyed!" }
  end
end

class AliasesTest < ActionController::TestCase
  tests StudentsController

  def setup
    draw_routes do
      resources :students
    end
  end

  def teardown
    clear_routes
  end

  def test_assignments_before_calling_alias
    Student.stubs(:new).returns(mock_student)
    get :new
    assert_response :success
    assert_equal 'magical', assigns(:something)
  end

  def test_controller_should_render_new
    Student.stubs(:new).returns(mock_student)
    get :new
    assert_response :success
    assert_equal 'New HTML', @response.body.strip
  end

  def test_expose_the_requested_user_on_edit
    Student.expects(:find).with('42').returns(mock_student)
    get :edit, params: { id: '42' }
    assert_equal mock_student, assigns(:student)
    assert_response :success
  end

  def test_controller_should_render_edit
    Student.stubs(:find).returns(mock_student)
    get :edit, params: { id: '42' }
    assert_response :success
    assert_equal 'Edit HTML', @response.body.strip
  end

  def test_render_xml_when_it_is_given_as_a_block
    @request.accept = 'application/xml'
    Student.stubs(:find).returns(mock_student)
    get :edit, params: { id: '42' }
    assert_response :success
    assert_equal 'Render XML', @response.body
  end

  def test_is_not_redirected_on_create_with_success_if_success_block_is_given
    Student.stubs(:new).returns(mock_student(save: true))
    @controller.stubs(:resource_url).returns('http://test.host/')
    post :create
    assert_response :success
    assert_equal "I won't redirect!", @response.body
  end

  def test_dumb_responder_quietly_receives_everything_on_failure
    @request.accept = 'text/html'
    Student.stubs(:new).returns(mock_student(save: false, errors: {some: :error}))
    @controller.stubs(:resource_url).returns('http://test.host/')
    post :create
    assert_response :success
    assert_equal "New HTML", @response.body.strip
  end

  def test_html_is_the_default_when_only_xml_is_overwriten
    @request.accept = '*/*'
    Student.stubs(:new).returns(mock_student(save: false, errors: {some: :error}))
    @controller.stubs(:resource_url).returns('http://test.host/')
    post :create
    assert_response :success
    assert_equal "New HTML", @response.body.strip
  end

  def test_wont_render_edit_template_on_update_with_failure_if_failure_block_is_given
    Student.stubs(:find).returns(mock_student(update: false, errors: { fail: true }))
    put :update, params: { id: '42' }
    assert_response :success
    assert_equal "I won't render!", @response.body
  end

  def test_dumb_responder_quietly_receives_everything_on_success
    Student.stubs(:find).returns(mock_student(update: true))
    @controller.stubs(:resource_url).returns('http://test.host/')
    put :update, params: { id: '42', student: {these: 'params'} }
    assert_equal mock_student, assigns(:student)
  end

  def test_block_is_called_when_student_is_destroyed
    Student.stubs(:find).returns(mock_student(destroy: true))
    delete :destroy, params: { id: '42' }
    assert_response :success
    assert_equal "Destroyed!", @response.body
  end

  def test_options_are_used_in_respond_with
    @request.accept = "application/xml"
    mock_student = mock_student(save: true, to_xml: "XML")
    Student.stubs(:new).returns(mock_student)

    post :create
    assert_equal "http://test.host/", @response.location
  end

  protected

    def mock_student(expectations={})
      @mock_student ||= begin
        student = mock(expectations.except(:errors))
        student.stubs(:class).returns(Student)
        student.stubs(:errors).returns(expectations.fetch(:errors, {}))
        student
      end
    end
end