Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. module ExpectationHelpers
  2. def expect_attributes attrs
  3. expect_json 'data.attributes', dasherize_keys(attrs)
  4. end
  5.  
  6. def expect_id id
  7. expect_json 'data', id: id
  8. end
  9.  
  10. def expect_type type
  11. expect_json 'data', type: type
  12. end
  13.  
  14. def expect_attributes_in_list attrs
  15. expect(json_body[:data]).to_not be_empty
  16. expect_json 'data.?.attributes', dasherize_keys(attrs)
  17. end
  18.  
  19. def expect_relationship attrs, in_list = false
  20. # If looking for item in a list, need to change location string
  21. location = if in_list
  22. "data.?.relationships.#{attrs[:key]}"
  23. else
  24. "data.relationships.#{attrs[:key]}"
  25. end
  26.  
  27. expect_json "#{location}.links.related", attrs[:link] if attrs[:link]
  28.  
  29. if attrs[:id]
  30. location = "#{location}.data"
  31. type = attrs[:type] || attrs[:key].pluralize
  32. id_value = attrs[:id]
  33. if id_value.respond_to? :each
  34. # if an array if ids were passed in, look for each of them in the list
  35. location = "#{location}.?"
  36. id_value.each do |id|
  37. # TODO: also look for included thing?
  38. expect_json location, type: type, id: id
  39. end
  40. else
  41. # otherwise just look for it
  42. # TODO: also look for included thing?
  43. expect_json location, type: type, id: id_value
  44. end
  45. end
  46. end
  47.  
  48. def expect_relationship_in_list attrs
  49. expect(json_body[:data]).to_not be_empty
  50. expect_relationship attrs, in_list: true
  51. end
  52.  
  53. def expect_item_to_not_be_in_list dont_find_me
  54. expect(json_body[:data]).to_not be_empty
  55. json_body[:data].each do |item|
  56. expect(item[:id]).to_not eq(dont_find_me.id)
  57. end
  58. end
  59.  
  60. def expect_item_count number
  61. expect_json_sizes data: number
  62. end
  63.  
  64. private
  65.  
  66. def dasherize_keys hash
  67. hash.transform_keys { |key| key.to_s.dasherize.to_sym }
  68. end
  69. end
  70.  
  71. RSpec.configure do |config|
  72. config.include ExpectationHelpers
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement