/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb

http://github.com/flyerhzm/rails_best_practices · Ruby · 186 lines · 168 code · 17 blank · 1 comment · 0 complexity · d9ae9668ee0fd5c453a94b9e12ec225b MD5 · raw file

  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. module RailsBestPractices
  4. module Reviews
  5. describe LawOfDemeterReview do
  6. let(:runner) do
  7. Core::Runner.new(
  8. prepares: [Prepares::ModelPrepare.new, Prepares::SchemaPrepare.new], reviews: described_class.new
  9. )
  10. end
  11. describe 'belongs_to' do
  12. before do
  13. content = <<-EOF
  14. class Invoice < ActiveRecord::Base
  15. belongs_to :user
  16. end
  17. EOF
  18. runner.prepare('app/models/invoice.rb', content)
  19. content = <<-EOF
  20. ActiveRecord::Schema.define(version: 20110216150853) do
  21. create_table "users", force => true do |t|
  22. t.string :name
  23. t.string :address
  24. t.string :cellphone
  25. end
  26. end
  27. EOF
  28. runner.prepare('db/schema.rb', content)
  29. end
  30. it 'laws of demeter with erb' do
  31. content = <<-EOF
  32. <%= @invoice.user.name %>
  33. <%= @invoice.user.address %>
  34. <%= @invoice.user.cellphone %>
  35. EOF
  36. runner.review('app/views/invoices/show.html.erb', content)
  37. expect(runner.errors.size).to eq(3)
  38. expect(runner.errors[0].to_s).to eq('app/views/invoices/show.html.erb:1 - law of demeter')
  39. end
  40. it 'laws of demeter with haml' do
  41. content = <<~EOF
  42. = @invoice.user.name
  43. = @invoice.user.address
  44. = @invoice.user.cellphone
  45. EOF
  46. runner.review('app/views/invoices/show.html.haml', content)
  47. expect(runner.errors.size).to eq(3)
  48. expect(runner.errors[0].to_s).to eq('app/views/invoices/show.html.haml:1 - law of demeter')
  49. end
  50. it 'laws of demeter with slim' do
  51. content = <<~EOF
  52. = @invoice.user.name
  53. = @invoice.user.address
  54. = @invoice.user.cellphone
  55. EOF
  56. runner.review('app/views/invoices/show.html.slim', content)
  57. expect(runner.errors.size).to eq(3)
  58. expect(runner.errors[0].to_s).to eq('app/views/invoices/show.html.slim:1 - law of demeter')
  59. end
  60. it 'noes law of demeter' do
  61. content = <<-EOF
  62. <%= @invoice.user_name %>
  63. <%= @invoice.user_address %>
  64. <%= @invoice.user_cellphone %>
  65. EOF
  66. runner.review('app/views/invoices/show.html.erb', content)
  67. expect(runner.errors.size).to eq(0)
  68. end
  69. end
  70. describe 'has_one' do
  71. before do
  72. content = <<-EOF
  73. class Invoice < ActiveRecord::Base
  74. has_one :price
  75. end
  76. EOF
  77. runner.prepare('app/models/invoice.rb', content)
  78. content = <<-EOF
  79. ActiveRecord::Schema.define(version: 20110216150853) do
  80. create_table "prices", force => true do |t|
  81. t.string :currency
  82. t.integer :number
  83. end
  84. end
  85. EOF
  86. runner.prepare('db/schema.rb', content)
  87. end
  88. it 'laws of demeter' do
  89. content = <<-EOF
  90. <%= @invoice.price.currency %>
  91. <%= @invoice.price.number %>
  92. EOF
  93. runner.review('app/views/invoices/show.html.erb', content)
  94. expect(runner.errors.size).to eq(2)
  95. expect(runner.errors[0].to_s).to eq('app/views/invoices/show.html.erb:1 - law of demeter')
  96. end
  97. end
  98. context 'polymorphic association' do
  99. before do
  100. content = <<-EOF
  101. class Comment < ActiveRecord::Base
  102. belongs_to :commentable, polymorphic: true
  103. end
  104. EOF
  105. runner.prepare('app/models/comment.rb', content)
  106. content = <<-EOF
  107. class Post < ActiveRecord::Base
  108. has_many :comments
  109. end
  110. EOF
  111. runner.prepare('app/models/comment.rb', content)
  112. content = <<-EOF
  113. ActiveRecord::Schema.define(version: 20110216150853) do
  114. create_table "posts", force => true do |t|
  115. t.string :title
  116. end
  117. end
  118. EOF
  119. runner.prepare('db/schema.rb', content)
  120. end
  121. it 'laws of demeter' do
  122. content = <<-EOF
  123. <%= @comment.commentable.title %>
  124. EOF
  125. runner.review('app/views/comments/index.html.erb', content)
  126. expect(runner.errors.size).to eq(1)
  127. expect(runner.errors[0].to_s).to eq('app/views/comments/index.html.erb:1 - law of demeter')
  128. end
  129. end
  130. it 'noes law of demeter with method call' do
  131. content = <<-EOF
  132. class Question < ActiveRecord::Base
  133. has_many :answers, dependent: :destroy
  134. end
  135. EOF
  136. runner.prepare('app/models/question.rb', content)
  137. content = <<-EOF
  138. class Answer < ActiveRecord::Base
  139. belongs_to :question, counter_cache: true, touch: true
  140. end
  141. EOF
  142. runner.prepare('app/models/answer.rb', content)
  143. content = <<-EOF
  144. class CommentsController < ApplicationController
  145. def comment_url
  146. question_path(@answer.question)
  147. end
  148. end
  149. EOF
  150. runner.review('app/controllers/comments_controller.rb', content)
  151. expect(runner.errors.size).to eq(0)
  152. end
  153. it 'does not check ignored files' do
  154. runner =
  155. Core::Runner.new(
  156. prepares: [Prepares::ModelPrepare.new, Prepares::SchemaPrepare.new],
  157. reviews: described_class.new(ignored_files: %r{app/views/invoices})
  158. )
  159. content = <<-EOF
  160. <%= @invoice.user.name %>
  161. <%= @invoice.user.address %>
  162. <%= @invoice.user.cellphone %>
  163. EOF
  164. runner.review('app/views/invoices/show.html.erb', content)
  165. expect(runner.errors.size).to eq(0)
  166. end
  167. end
  168. end
  169. end