PageRenderTime 26ms CodeModel.GetById 9ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb

http://github.com/flyerhzm/rails_best_practices
Ruby | 86 lines | 77 code | 8 blank | 1 comment | 0 complexity | 6b3e10fc3d76c812fc395e8416b92625 MD5 | raw file
 1# frozen_string_literal: true
 2
 3require 'spec_helper'
 4
 5module RailsBestPractices
 6  module Reviews
 7    describe OveruseRouteCustomizationsReview do
 8      let(:runner) { Core::Runner.new(reviews: described_class.new) }
 9
10      it 'overuses route customizations' do
11        content = <<-EOF
12        RailsBestpracticesCom::Application.routes.draw do
13          resources :posts do
14            member do
15              post :create_comment
16              update :update_comment
17              delete :delete_comment
18            end
19
20            collection do
21              get :comments
22            end
23          end
24        end
25        EOF
26        runner.review('config/routes.rb', content)
27        expect(runner.errors.size).to eq(1)
28        expect(runner.errors[0].to_s).to eq('config/routes.rb:2 - overuse route customizations (customize_count > 3)')
29      end
30
31      it 'overuses route customizations another way' do
32        content = <<-EOF
33        RailsBestpracticesCom::Application.routes.draw do
34          resources :posts do
35            post :create_comment, on: :member
36            update :update_comment, on: :member
37            delete :delete_comment, on: :member
38            get :comments, on: :collection
39          end
40        end
41        EOF
42        runner.review('config/routes.rb', content)
43        expect(runner.errors.size).to eq(1)
44        expect(runner.errors[0].to_s).to eq('config/routes.rb:2 - overuse route customizations (customize_count > 3)')
45      end
46
47      it 'does not overuse route customizations without customization' do
48        content = <<-EOF
49        RailsBestpracticesCom::Application.routes.draw do
50          resources :posts
51        end
52        EOF
53        runner.review('config/routes.rb', content)
54        expect(runner.errors.size).to eq(0)
55      end
56
57      it 'does not overuse route customizations when customize route is only one' do
58        content = <<-EOF
59        RailsBestpracticesCom::Application.routes.draw do
60          resources :posts do
61            member do
62              post :vote
63            end
64          end
65        end
66        EOF
67        runner.review('config/routes.rb', content)
68        expect(runner.errors.size).to eq(0)
69      end
70
71      it 'does not check ignored files' do
72        runner = Core::Runner.new(reviews: described_class.new(ignored_files: %r{config/routes\.rb}))
73        content = <<-EOF
74          ActionController::Routing::Routes.draw do |map|
75            map.resources :posts, member: { comments: :get,
76                                               create_comment: :post,
77                                               update_comment: :update,
78                                               delete_comment: :delete }
79          end
80        EOF
81        runner.review('config/routes.rb', content)
82        expect(runner.errors.size).to eq(0)
83      end
84    end
85  end
86end