开发者

Rails 2.3.8 testing: How to have my method for *every* assertion?

I have a helper module shared by all of my Rails 2.3.8 app's tests. It provides setup and teardown methods, among other things. From looking at the code, it appears as though I should be able to define a method named add_assertion that will be called by _wrap_assertion (Look for def _wrap_assertion in Test::Unit::Assertions 2.1.2.) Unfortunately, that doesn't appear to be happening.

Setting a breakpoint in the setup method in my helper module, I can verify that Test::Unit::Assertions is in its ancestry:

(rdb:1) self.class                    
RoutingTest                           
(rdb:1) self.class.ancestors          
[RoutingTest, CandlepinRequestHelper, ActionController::IntegrationTest, \
ActionController::Integration::Runner, ActiveSupport::TestCase, \
ActiveSupport::Testing::Deprecation, ActiveSupport::Testing::Assertions, \
ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit, \
ActiveSupport::Callbacks, ActiveSupport::Testing::SetupAndTeardown, \
ActiveSupport::Testing::Default, Test::Unit::TestCase, \
Mocha::Integration::TestUnit::RubyVersion186AndAbove, Mocha::API, \
Mocha::ParameterMatchers, Test::Unit::Util::BacktraceFilter, \
Test::Unit::Assertions, Object, Mocha::ObjectMethods, Socket::Constants, \
InstanceExecHelper, JSON::Ext::Generator::GeneratorMethods::Object, \
ActiveSupport::Dependencies::Loadable, InstanceExecMethods, \
Base64::Deprecated开发者_如何学Python, Base64, PP::ObjectMixin, Kernel]
(rdb:1) self.class.ancestors.include?(Test::Unit::Assertions)
true

but my def add_assertion method is being ignored.

So what is the recommended way to insert a 'call my method for every assertion' point in the Rails 2.3.8 testing sequence?

Thanks!


Perhaps there's already a suitable hook mechanism for you but I'm sorry to say I don't know it. Could you perhaps create a base class for your test cases, define setup there and then remember to call super should you need to override it in your tests?

EDIT 1

Sorry, I missed the point about needing to hook in to assertions specifically. I realise this doesn't closely match your environment but using rails 3 and ruby 1.9, I've gotten the following to run:

test/test_helper.rb

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

  def assert_with_beards(*args)
    puts "Size 8"
    assert_without_beards(*args)
  end 

  alias_method_chain :assert, :beards

  # Add more helper methods to be used by all tests here...
end

Running a basic assert true style test produces:

(in /Users/noodl/temp)
Loaded suite /Users/noodl/.rvm/gems/ruby-1.9.2-p136@rails30/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
Size 8
.
Finished in 0.050746 seconds.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

Usefully, all the assertions I've seen in rails and in the various ruby libraries all ultimately call assert in one way or other.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜