dirname(_ _FILE_ _) + '/../lib/'
require 'http_authentication'
??? Similarly, the plugin??™s init.rb file is not loaded, so you must set up anything your
tests need, such as including your plugin??™s modules in the TestCase class:
class HttpBasicAuthenticationTest < Test::Unit::TestCase
include HttpAuthentication::Basic
# ...
end
??? You must usually recreate (mock or stub) any Rails functionality involved in
your test. In the case of the HTTPAuthentication plugin, it would be too much
overhead to load the entire ActionController framework for the tests. The functionality
being tested is very simple, and requires very little of ActionController:
def test_authentication_request
authentication_request(@controller, "Megaglobalapp")
assert_equal 'Basic realm="Megaglobalapp"',
@controller.headers["WWW-Authenticate"]
assert_equal :unauthorized, @controller.renders.first[:status]
end
To support this limited subset of ActionController??™s features, the test??™s setup
method creates a stub controller:
def setup
@controller = Class.new do
attr_accessor :headers, :renders
def initialize
@headers, @renders = {}, []
end
def request
Class.new do
def env
{'HTTP_AUTHORIZATION' =>
HttpAuthentication::Basic.encode_credentials("dhh", "secret") }
end
end.new
end
def render(options)
self.renders << options
end
end.new
end
92 | Chapter 3: Rails Plugins
The Class.
Pages:
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145