Guest User

Untitled

a guest
Jun 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. require 'rails_helper'
  2.  
  3. RSpec.describe UsersController, type: :controller do
  4.  
  5. describe 'before actions' do
  6. describe 'load_user' do
  7. it 'is expected to define before action' do
  8. is_expected.to use_before_action(:load_user)
  9. end
  10. end
  11. end
  12.  
  13. # index action
  14. describe 'GET #index' do
  15. before do
  16. get :index
  17. end
  18.  
  19. it 'is expected to assign user instance variable' do
  20. expect(assigns[:users]).to eq(User.all)
  21. end
  22. end
  23.  
  24. # new action
  25. describe 'GET #new' do
  26. before do
  27. get :new
  28. end
  29.  
  30. it 'is expected to assign user as new instance variable' do
  31. expect(assigns[:user]).to be_instance_of(User)
  32. end
  33.  
  34. it 'renders new template' do
  35. is_expected.to render_template(:new)
  36. end
  37.  
  38. it 'renders application layout' do
  39. is_expected.to render_template(:application)
  40. end
  41. end
  42.  
  43. # create action
  44. describe 'POST #create' do
  45. before do
  46. post :create, params: params
  47. end
  48.  
  49. context 'when params are correct' do
  50. let(:params) { { user: { name: "Abhishek kanojia" } } }
  51.  
  52. it 'is expected to create new user successfully' do
  53. expect(assigns[:user]).to be_instance_of(User)
  54. end
  55.  
  56. it 'is expected to have name assigned to it' do
  57. expect(assigns[:user].name).to eq('Abhishek kanojia')
  58. end
  59.  
  60. it 'is expected to redirect to users path' do
  61. is_expected.to redirect_to users_path
  62. end
  63.  
  64. it 'is expected to set flash message' do
  65. expect(flash[:notice]).to eq('User Created Successfully.')
  66. end
  67. end
  68.  
  69. context 'when params are not correct' do
  70. let(:params) { { user: { name: '' } } }
  71.  
  72. it 'is expected to render new template' do
  73. is_expected.to render_template(:new)
  74. end
  75.  
  76. it 'is expected to add errors to name attribute' do
  77. expect(assigns[:user].errors[:name]).to eq(['can\'t be blank'])
  78. end
  79. end
  80. end
  81.  
  82. # edit action
  83. describe 'GET #edit' do
  84. before do
  85. # something that you want to execute before running `it` block
  86. get :edit, params: params
  87. end
  88.  
  89. context 'when user id is valid' do
  90. let(:user) { FactoryBot.create :user }
  91. let(:params) { { id: user.id } }
  92.  
  93. it 'is expected to set user instance variable' do
  94. expect(assigns[:user]).to eq(User.find_by(id: params[:id]))
  95. end
  96.  
  97. it 'is expected to render edit template' do
  98. is_expected.to render_template(:edit)
  99. end
  100. end
  101.  
  102. context 'when user id is invalid' do
  103. let(:params) { { id: Faker::Number.number } }
  104.  
  105. it 'is expected to redirect_to users path' do
  106. is_expected.to redirect_to(users_path)
  107. end
  108.  
  109. it 'is expected to set flash' do
  110. expect(flash[:notice]).to eq('User not found.')
  111. end
  112. end
  113.  
  114. end
  115.  
  116. # update action
  117. describe 'PATCH #update' do
  118.  
  119. before do
  120. # something that you want to execute before running `it` block
  121. patch :update, params: params
  122. end
  123.  
  124. context 'when user not found' do
  125. let(:params) { { id: Faker::Number.number } }
  126.  
  127. it 'is expected to redirect_to users path' do
  128. is_expected.to redirect_to(users_path)
  129. end
  130.  
  131. it 'is expected to set flash' do
  132. expect(flash[:notice]).to eq('User not found.')
  133. end
  134. end
  135.  
  136. context 'when user exist in database' do
  137. let(:user) { FactoryBot.create :user }
  138. let(:params) { { id: user.id, user: { name: 'test name' } } }
  139.  
  140. context 'when data is provided is valid' do
  141. it 'is expected to update user' do
  142. expect(user.reload.name).to eq('test name')
  143. end
  144.  
  145. it 'is_expected to redirect_to users_path' do
  146. is_expected.to redirect_to(users_path)
  147. end
  148.  
  149. it 'is expected to set flash message' do
  150. expect(flash[:notice]).to eq('User has been updated Successfully.')
  151. end
  152. end
  153.  
  154. context 'when data is invalid' do
  155. let(:user) { FactoryBot.create :user }
  156. let(:params) { { id: user.id, user: { name: '' } } }
  157.  
  158. it 'is expected not to update user name' do
  159. expect(user.reload.name).not_to be_empty
  160. end
  161.  
  162. it 'is expected to render edit template' do
  163. expect(response).to render_template(:edit)
  164. end
  165.  
  166. it 'is expected to add errors to user name attribute' do
  167. expect(assigns[:user].errors[:name]).to eq(['can\'t be blank'])
  168. end
  169. end
  170. end
  171. end
  172.  
  173. end
Add Comment
Please, Sign In to add comment