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

/spec/rails_best_practices/core/checking_visitor_spec.rb

http://github.com/flyerhzm/rails_best_practices
Ruby | 78 lines | 67 code | 10 blank | 1 comment | 0 complexity | 8be9c5f09eef9c19009e8eaea71053e6 MD5 | raw file
 1# encoding: utf-8
 2require 'spec_helper'
 3
 4class TestLexical1
 5end
 6class TestLexical2
 7end
 8class TestPrepare1 < RailsBestPractices::Core::Check
 9  interesting_nodes :call
10  interesting_files MODEL_FILES
11end
12
13class TestPrepare2 < RailsBestPractices::Core::Check
14  interesting_nodes :class
15  interesting_files MAILER_FILES
16end
17
18class TestReview1 < RailsBestPractices::Core::Check
19  interesting_nodes :defn
20  interesting_files CONTROLLER_FILES
21end
22
23class TestReview2 < RailsBestPractices::Core::Check
24  interesting_nodes :call
25  interesting_files VIEW_FILES
26end
27
28describe RailsBestPractices::Core::CheckingVisitor do
29  let(:lexical1) { TestLexical1.new }
30  let(:lexical2) { TestLexical2.new }
31  let(:prepare1) { TestPrepare1.new }
32  let(:prepare2) { TestPrepare2.new }
33  let(:review1) { TestReview1.new }
34  let(:review2) { TestReview2.new }
35  let(:visitor) {
36    RailsBestPractices::Core::CheckingVisitor.new(
37      :lexicals => [lexical1, lexical2],
38      :prepares => [prepare1, prepare2],
39      :reviews => [review1, review2]
40    )
41  }
42
43  it "should lexical check" do
44    filename = "app/models/user.rb"
45    content = "class User; end"
46    lexical1.should_receive(:check).with(filename, content)
47    lexical2.should_receive(:check).with(filename, content)
48    visitor.lexical(filename, content)
49  end
50
51  it "should prepare model associations" do
52    node = stub(:sexp_type => :call, :children => [], :file => "app/models/user.rb")
53    prepare1.should_receive(:node_start).with(node)
54    prepare1.should_receive(:node_end).with(node)
55    visitor.prepare(node)
56  end
57
58  it "should prepare mailer names" do
59    node = stub(:sexp_type => :class, :children => [], :file => "app/mailers/user_mailer.rb")
60    prepare2.should_receive(:node_start).with(node)
61    prepare2.should_receive(:node_end).with(node)
62    visitor.prepare(node)
63  end
64
65  it "should review controller method definitions" do
66    node = stub(:sexp_type => :defn, :children => [], :file => "app/controllers/users_controller.rb")
67    review1.should_receive(:node_start).with(node)
68    review1.should_receive(:node_end).with(node)
69    visitor.review(node)
70  end
71
72  it "should review view calls" do
73    node = stub(:sexp_type => :call, :children => [], :file => "app/views/users/new.html.erb")
74    review2.should_receive(:node_start).with(node)
75    review2.should_receive(:node_end).with(node)
76    visitor.review(node)
77  end
78end