| 1 |
module Spec |
| 2 |
module Rails |
| 3 |
module Example |
| 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 |
class ControllerExampleGroup < FunctionalExampleGroup |
| 65 |
class << self |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
def integrate_views(integrate_views = true) |
| 76 |
@integrate_views = integrate_views |
| 77 |
end |
| 78 |
|
| 79 |
def integrate_views? |
| 80 |
@integrate_views |
| 81 |
end |
| 82 |
|
| 83 |
def inherited(klass) |
| 84 |
klass.controller_class_name = controller_class_name |
| 85 |
klass.integrate_views(integrate_views?) |
| 86 |
super |
| 87 |
end |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
def controller_name(name) |
| 96 |
@controller_class_name = "#{name}_controller".camelize |
| 97 |
end |
| 98 |
attr_accessor :controller_class_name |
| 99 |
end |
| 100 |
|
| 101 |
before(:each) do |
| 102 |
|
| 103 |
if defined?(ActionMailer) |
| 104 |
@deliveries = [] |
| 105 |
ActionMailer::Base.deliveries = @deliveries |
| 106 |
end |
| 107 |
|
| 108 |
unless @controller.class.ancestors.include?(ActionController::Base) |
| 109 |
Spec::Expectations.fail_with <<-EOE |
| 110 |
You have to declare the controller name in controller specs. For example: |
| 111 |
describe "The ExampleController" do |
| 112 |
controller_name "example" #invokes the ExampleController |
| 113 |
end |
| 114 |
EOE |
| 115 |
end |
| 116 |
(class << @controller; self; end).class_eval do |
| 117 |
def controller_path |
| 118 |
self.class.name.underscore.gsub('_controller', '') |
| 119 |
end |
| 120 |
include ControllerInstanceMethods |
| 121 |
end |
| 122 |
@controller.integrate_views! if @integrate_views |
| 123 |
@controller.session = session |
| 124 |
end |
| 125 |
|
| 126 |
attr_reader :response, :request, :controller |
| 127 |
|
| 128 |
def initialize(defined_description, &implementation) |
| 129 |
super |
| 130 |
controller_class_name = self.class.controller_class_name |
| 131 |
if controller_class_name |
| 132 |
@controller_class_name = controller_class_name.to_s |
| 133 |
else |
| 134 |
@controller_class_name = self.class.described_type.to_s |
| 135 |
end |
| 136 |
@integrate_views = self.class.integrate_views? |
| 137 |
end |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
def route_for(options) |
| 145 |
ensure_that_routes_are_loaded |
| 146 |
ActionController::Routing::Routes.generate(options) |
| 147 |
end |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
def params_from(method, path) |
| 155 |
ensure_that_routes_are_loaded |
| 156 |
ActionController::Routing::Routes.recognize_path(path, :method => method) |
| 157 |
end |
| 158 |
|
| 159 |
protected |
| 160 |
def _assigns_hash_proxy |
| 161 |
@_assigns_hash_proxy ||= AssignsHashProxy.new @controller |
| 162 |
end |
| 163 |
|
| 164 |
private |
| 165 |
def ensure_that_routes_are_loaded |
| 166 |
ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? |
| 167 |
end |
| 168 |
|
| 169 |
module ControllerInstanceMethods |
| 170 |
include Spec::Rails::Example::RenderObserver |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
def render(options=nil, deprecated_status_or_extra_options=nil, &block) |
| 178 |
if ::Rails::VERSION::STRING >= '2.0.0' && deprecated_status_or_extra_options.nil? |
| 179 |
deprecated_status_or_extra_options = {} |
| 180 |
end |
| 181 |
|
| 182 |
unless block_given? |
| 183 |
unless integrate_views? |
| 184 |
if @template.respond_to?(:finder) |
| 185 |
(class << @template.finder; self; end).class_eval do |
| 186 |
define_method :file_exists? do |
| 187 |
true |
| 188 |
end |
| 189 |
end |
| 190 |
else |
| 191 |
(class << @template; self; end).class_eval do |
| 192 |
define_method :file_exists? do |
| 193 |
true |
| 194 |
end |
| 195 |
end |
| 196 |
end |
| 197 |
(class << @template; self; end).class_eval do |
| 198 |
define_method :render_file do |*args| |
| 199 |
@first_render ||= args[0] |
| 200 |
end |
| 201 |
end |
| 202 |
end |
| 203 |
end |
| 204 |
|
| 205 |
if matching_message_expectation_exists(options) |
| 206 |
expect_render_mock_proxy.render(options, &block) |
| 207 |
@performed_render = true |
| 208 |
else |
| 209 |
unless matching_stub_exists(options) |
| 210 |
super(options, deprecated_status_or_extra_options, &block) |
| 211 |
end |
| 212 |
end |
| 213 |
end |
| 214 |
|
| 215 |
private |
| 216 |
def matching_message_expectation_exists(options) |
| 217 |
expect_render_mock_proxy.send(:__mock_proxy).send(:find_matching_expectation, :render, options) |
| 218 |
end |
| 219 |
|
| 220 |
def matching_stub_exists(options) |
| 221 |
expect_render_mock_proxy.send(:__mock_proxy).send(:find_matching_method_stub, :render, options) |
| 222 |
end |
| 223 |
|
| 224 |
public |
| 225 |
if self.respond_to?(:should_receive) && self.respond_to?(:stub!) |
| 226 |
self.send :alias_method, :orig_should_receive, :should_receive |
| 227 |
self.send :alias_method, :orig_stub!, :stub! |
| 228 |
def raise_with_disable_message(old_method, new_method) |
| 229 |
raise %Q| |
| 230 |
controller. |
| 231 |
can often produce unexpected results. Instead, you should |
| 232 |
use the following (before the action): |
| 233 |
|
| 234 |
controller. |
| 235 |
|
| 236 |
See the rdoc for |
| 237 |
| |
| 238 |
end |
| 239 |
def should_receive(*args) |
| 240 |
if args[0] == :render |
| 241 |
raise_with_disable_message("should_receive", "expect_render") |
| 242 |
else |
| 243 |
orig_should_receive(*args) |
| 244 |
end |
| 245 |
end |
| 246 |
def stub!(*args) |
| 247 |
if args[0] == :render |
| 248 |
raise_with_disable_message("stub!", "stub_render") |
| 249 |
else |
| 250 |
orig_stub!(*args) |
| 251 |
end |
| 252 |
end |
| 253 |
end |
| 254 |
|
| 255 |
def response(&block) |
| 256 |
|
| 257 |
@update = block |
| 258 |
@_response || @response |
| 259 |
end |
| 260 |
|
| 261 |
def integrate_views! |
| 262 |
@integrate_views = true |
| 263 |
end |
| 264 |
|
| 265 |
private |
| 266 |
|
| 267 |
def integrate_views? |
| 268 |
@integrate_views |
| 269 |
end |
| 270 |
end |
| 271 |
|
| 272 |
Spec::Example::ExampleGroupFactory.register(:controller, self) |
| 273 |
end |
| 274 |
end |
| 275 |
end |
| 276 |
end |