Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. # spec/controllers/api/v1/users_controller_spec.rb
  2. require 'rails_helper'
  3.  
  4. RSpec.describe Api::V1::UsersController, type: :controller do # changed to Api::V1:: like the path spec/controllers/api/v1/
  5. before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" } # run before each test in this scope
  6.  
  7. describe "GET #show" do
  8. ...
  9. end
  10.  
  11. describe "POST #create" do
  12.  
  13. context "when is succesfully created" do
  14. before(:each) do
  15. @user_attributes = FactoryGirl.attributes_for :user
  16. # second argument to `post` must be a value to `params` hash to pass the tests
  17. post :create, params: { user: @user_attributes }, format: :json
  18. end
  19.  
  20. it "renders the json representation for the user record just created" do
  21. user_response = JSON.parse(response.body, symbolize_names: true)
  22. expect(user_response[:email]).to eql @user_attributes[:email]
  23. end
  24.  
  25. it { should respond_with 201 }
  26. end
  27.  
  28. context "when is not created" do
  29. before(:each) do
  30. @invalid_user_attributes = {password: "12345678", password_confirmation: "12345678" }
  31. # second argument to `post` must be a value to `params` hash to pass the tests
  32. post :create, params: { user: @invalid_user_attributes }, format: :json
  33. end
  34.  
  35. it "renders an errors json" do
  36. user_response = JSON.parse(response.body, symbolize_names: true)
  37. expect(user_response).to have_key(:errors)
  38. end
  39.  
  40. it "renders the json errors on why the user could not be created" do
  41. user_response = JSON.parse(response.body, symbolize_names: true)
  42. expect(user_response[:errors][:email]).to include "can't be blank"
  43. end
  44.  
  45. it { should respond_with 422 }
  46. end
  47. end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement