/
controller_example_group_spec.rb
75 lines (63 loc) · 2.29 KB
/
controller_example_group_spec.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
require "spec_helper"
module RSpec::Rails
describe ControllerExampleGroup do
it { should be_included_in_files_in('./spec/controllers/') }
it { should be_included_in_files_in('.\\spec\\controllers\\') }
let(:group) do
RSpec::Core::ExampleGroup.describe do
include ControllerExampleGroup
end
end
it "includes routing matchers" do
group.included_modules.should include(RSpec::Rails::Matchers::RoutingMatchers)
end
it "adds :type => :controller to the metadata" do
group.metadata[:type].should eq(:controller)
end
context "with implicit subject" do
it "uses the controller as the subject" do
controller = double('controller')
example = group.new
example.stub(:controller => controller)
example.subject.should == controller
end
end
context "with explicit subject" do
it "uses the specified subject instead of the controller" do
group.subject { 'explicit' }
example = group.new
example.subject.should == 'explicit'
end
end
describe "#controller" do
before do
group.class_eval do
controller(Class.new) { }
end
end
it "delegates named route helpers to the underlying controller" do
controller = double('controller')
controller.stub(:foos_url).and_return('http://test.host/foos')
example = group.new
example.stub(:controller => controller)
# As in the routing example spec, this is pretty invasive, but not sure
# how to do it any other way as the correct operation relies on before
# hooks
routes = ActionDispatch::Routing::RouteSet.new
routes.draw { resources :foos }
example.instance_variable_set(:@orig_routes, routes)
example.foos_url.should eq('http://test.host/foos')
end
end
describe "#bypass_rescue" do
it "overrides the rescue_with_handler method on the controller to raise submitted error" do
example = group.new
example.instance_variable_set("@controller", Class.new { def rescue_with_handler(e); end }.new)
example.bypass_rescue
expect do
example.controller.rescue_with_handler(RuntimeError.new("foo"))
end.to raise_error("foo")
end
end
end
end