PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/controllers/content_types_controller_spec.rb

https://github.com/next-l/enju_leaf
Ruby | 174 lines | 125 code | 21 blank | 28 comment | 0 complexity | 61cd17764cf5a3ddb36a3f68f3e1d1d8 MD5 | raw file
  1. require 'rails_helper'
  2. # This spec was generated by rspec-rails when you ran the scaffold generator.
  3. # It demonstrates how one might use RSpec to specify the controller code that
  4. # was generated by Rails when you ran the scaffold generator.
  5. #
  6. # It assumes that the implementation code is generated by the rails scaffold
  7. # generator. If you are using any extension libraries to generate different
  8. # controller code, this generated spec may or may not pass.
  9. #
  10. # It only uses APIs available in rails and/or rspec-rails. There are a number
  11. # of tools you can use to make these specs even more expressive, but we're
  12. # sticking to rails and rspec-rails APIs to keep things simple and stable.
  13. #
  14. # Compared to earlier versions of this generator, there is very limited use of
  15. # stubs and message expectations in this spec. Stubs are only used when there
  16. # is no simpler way to get a handle on the object needed for the example.
  17. # Message expectations are only used when there is no simpler way to specify
  18. # that an instance is receiving a specific message.
  19. describe ContentTypesController do
  20. fixtures :all
  21. login_fixture_admin
  22. # This should return the minimal set of attributes required to create a valid
  23. # ContentType. As you add validations to ContentType, be sure to
  24. # update the return value of this method accordingly.
  25. def valid_attributes
  26. FactoryBot.attributes_for(:content_type)
  27. end
  28. describe 'GET index' do
  29. it 'assigns all content_types as @content_types' do
  30. content_type = ContentType.create! valid_attributes
  31. get :index
  32. expect(assigns(:content_types)).to eq(ContentType.order(:position))
  33. end
  34. end
  35. describe 'GET show' do
  36. it 'assigns the requested content_type as @content_type' do
  37. content_type = ContentType.create! valid_attributes
  38. get :show, params: { id: content_type.id }
  39. expect(assigns(:content_type)).to eq(content_type)
  40. end
  41. end
  42. describe 'GET new' do
  43. it 'assigns a new content_type as @content_type' do
  44. get :new
  45. expect(assigns(:content_type)).to be_a_new(ContentType)
  46. end
  47. end
  48. describe 'GET edit' do
  49. it 'assigns the requested content_type as @content_type' do
  50. content_type = ContentType.create! valid_attributes
  51. get :edit, params: { id: content_type.id }
  52. expect(assigns(:content_type)).to eq(content_type)
  53. end
  54. it 'assigns the content_type even if it associates manifestation(s)' do
  55. content_type = FactoryBot.create(:content_type)
  56. manifestation = FactoryBot.create(:manifestation, content_type_id: content_type.id)
  57. get :edit, params: { id: content_type.id }
  58. expect(assigns(:content_type)).to eq content_type
  59. expect(response).to be_successful
  60. end
  61. end
  62. describe 'POST create' do
  63. describe 'with valid params' do
  64. it 'creates a new ContentType' do
  65. expect do
  66. post :create, params: { content_type: valid_attributes }
  67. end.to change(ContentType, :count).by(1)
  68. end
  69. it 'assigns a newly created content_type as @content_type' do
  70. post :create, params: { content_type: valid_attributes }
  71. expect(assigns(:content_type)).to be_a(ContentType)
  72. expect(assigns(:content_type)).to be_persisted
  73. end
  74. it 'redirects to the created content_type' do
  75. post :create, params: { content_type: valid_attributes }
  76. expect(response).to redirect_to(ContentType.last)
  77. end
  78. end
  79. describe 'with invalid params' do
  80. it 'assigns a newly created but unsaved content_type as @content_type' do
  81. # Trigger the behavior that occurs when invalid params are submitted
  82. ContentType.any_instance.stub(:save).and_return(false)
  83. post :create, params: { content_type: { name: 'test' } }
  84. expect(assigns(:content_type)).to be_a_new(ContentType)
  85. end
  86. it "re-renders the 'new' template" do
  87. # Trigger the behavior that occurs when invalid params are submitted
  88. ContentType.any_instance.stub(:save).and_return(false)
  89. post :create, params: { content_type: { name: 'test' } }
  90. expect(response).to render_template('new')
  91. end
  92. end
  93. end
  94. describe 'PUT update' do
  95. describe 'with valid params' do
  96. it 'updates the requested content_type' do
  97. content_type = ContentType.create! valid_attributes
  98. # Assuming there are no other content_types in the database, this
  99. # specifies that the ContentType created on the previous line
  100. # receives the :update message with whatever params are
  101. # submitted in the request.
  102. ContentType.any_instance.should_receive(:update).with('name' => 'test')
  103. put :update, params: { id: content_type.id, content_type: { 'name' => 'test' } }
  104. end
  105. it 'assigns the requested content_type as @content_type' do
  106. content_type = ContentType.create! valid_attributes
  107. put :update, params: { id: content_type.id, content_type: valid_attributes }
  108. expect(assigns(:content_type)).to eq(content_type)
  109. end
  110. it 'redirects to the content_type' do
  111. content_type = ContentType.create! valid_attributes
  112. put :update, params: { id: content_type.id, content_type: valid_attributes }
  113. expect(response).to redirect_to(content_type)
  114. end
  115. it 'moves its position when specified' do
  116. content_type = ContentType.create! valid_attributes
  117. position = content_type.position
  118. put :update, params: { id: content_type.id, move: 'higher' }
  119. expect(response).to redirect_to content_types_url
  120. assigns(:content_type).reload.position.should eq position - 1
  121. end
  122. end
  123. describe 'with invalid params' do
  124. it 'assigns the content_type as @content_type' do
  125. content_type = ContentType.create! valid_attributes
  126. # Trigger the behavior that occurs when invalid params are submitted
  127. ContentType.any_instance.stub(:save).and_return(false)
  128. put :update, params: { id: content_type.id, content_type: { name: 'test' } }
  129. expect(assigns(:content_type)).to eq(content_type)
  130. end
  131. it "re-renders the 'edit' template" do
  132. content_type = ContentType.create! valid_attributes
  133. # Trigger the behavior that occurs when invalid params are submitted
  134. ContentType.any_instance.stub(:save).and_return(false)
  135. put :update, params: { id: content_type.id, content_type: { name: 'test' } }
  136. expect(response).to render_template('edit')
  137. end
  138. end
  139. end
  140. describe 'DELETE destroy' do
  141. it 'destroys the requested content_type' do
  142. content_type = ContentType.create! valid_attributes
  143. expect do
  144. delete :destroy, params: { id: content_type.id }
  145. end.to change(ContentType, :count).by(-1)
  146. end
  147. it 'redirects to the content_types list' do
  148. content_type = ContentType.create! valid_attributes
  149. delete :destroy, params: { id: content_type.id }
  150. expect(response).to redirect_to(content_types_url)
  151. end
  152. end
  153. end