-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
controller_example_group.rb
109 lines (103 loc) · 2.9 KB
/
controller_example_group.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'action_controller'
require 'webrat'
module RSpec::Rails
# Extends ActionController::TestCase::Behavior to work with RSpec.
#
# == Examples
#
# == with stubs
#
# describe WidgetsController do
# describe "GET index" do
# it "assigns all widgets to @widgets" do
# widget = stub_model(Widget)
# Widget.stub(:all) { widget }
# get :index
# assigns(:widgets).should eq([widget])
# end
# end
# end
#
# === with a factory
#
# describe WidgetsController do
# describe "GET index" do
# it "assigns all widgets to @widgets" do
# widget = Factory(:widget)
# get :index
# assigns(:widgets).should eq([widget])
# end
# end
# end
#
# === with fixtures
#
# describe WidgetsController do
# describe "GET index" do
# fixtures :widgets
#
# it "assigns all widgets to @widgets" do
# get :index
# assigns(:widgets).should eq(Widget.all)
# end
# end
# end
#
# == Matchers
#
# In addition to the stock matchers from rspec-expectations, controller
# specs add these matchers, which delegate to rails' assertions:
#
# response.should render_template(*args)
# => delegates to assert_template(*args)
#
# response.should redirect_to(destination)
# => delegates to assert_redirected_to(destination)
#
# == Isolation from views
#
# RSpec's preferred approach to spec'ing controller behaviour is to isolate
# the controller from its collaborators. By default, therefore, controller
# example groups do not render views. This means that a view template need
# not even exist in order to run a controller spec, and you can still specify
# which template the controller should render.
#
# == View rendering
#
# If you prefer a more integrated approach, similar to that of
# Rails' functional tests, you can tell controller groups to
# render views with the +render_views+ declaration:
#
# describe WidgetsController do
# render_views
# ...
#
module ControllerExampleGroup
extend ActiveSupport::Concern
include RSpec::Rails::SetupAndTeardownAdapter
include RSpec::Rails::TestUnitAssertionAdapter
include ActionController::TestCase::Behavior
include RSpec::Rails::ViewRendering
include Webrat::Matchers
include Webrat::Methods
include RSpec::Matchers
include RSpec::Rails::ControllerSpecMatchers
module ClassMethods
def controller_class
describes
end
end
module InstanceMethods
attr_reader :controller
end
included do
before do
@routes = ::Rails.application.routes
ActionController::Base.allow_forgery_protection = false
end
end
RSpec.configure do |c|
c.include self, :example_group => { :file_path => /\bspec\/controllers\// }
end
end
end