Prev | Current Page 135 | Next

Brad Ediger

"Advanced Rails"

dirname(_ _FILE_ _)}/../lib/ssl_requirement"
ActionController::Base.logger = nil
ActionController::Routing::Routes.reload rescue nil
Finally come the test controller and test case??”these follow much the same format
as Rails functional tests, as we have done all of the setup manually.
class SslRequirementController < ActionController::Base
include SslRequirement
ssl_required :a, :b
ssl_allowed :c
# action definitions...
end
Testing Plugins | 93
class SslRequirementTest < Test::Unit::TestCase
def setup
@controller = SslRequirementController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
# tests...
end
The Deadlock Retry plugin, another standard Rails plugin designed to retry deadlocked
database transactions, provides a good example of how to stub out an
ActiveRecord model class:*
class MockModel
def self.transaction(*objects, &block)
block.call
end
def self.logger
@logger ||= Logger.new(nil)
end
include DeadlockRetry
end
This allows simple features to be tested without introducing a database dependency:
def test_error_if_limit_exceeded
assert_raise(ActiveRecord::StatementInvalid) do
MockModel.transaction { raise ActiveRecord::StatementInvalid,
DEADLOCK_ERROR }
end
end
Testing Plugin Database Dependencies
The semantics of some plugins makes them difficult to test without relying on a database.


Pages:
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147