Rails 2.3.8测试:如何使用* every *断言的方法?

时间:2011-02-08 17:54:16

标签: ruby-on-rails ruby testing

我有一个由我的所有Rails 2.3.8应用测试共享的辅助模块。除其他外,它提供setupteardown方法。通过查看代码,似乎我应该能够定义一个名为add_assertion的方法,该方法将由_wrap_assertion调用(查找def _wrap_assertion in Test::Unit::Assertions 2.1.2。)不幸的是,这似乎并没有发生。

在我的帮助模块的setup方法中设置断点,我可以验证Test::Unit::Assertions是否在其祖先中:

(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, Base64, PP::ObjectMixin, Kernel]
(rdb:1) self.class.ancestors.include?(Test::Unit::Assertions)
true

但我的def add_assertion方法被忽略了。

那么在Rails 2.3.8测试序列中插入“为每个断言调用我的方法”点的推荐方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

也许已经有适合您的钩子机制,但我很遗憾地说我不知道​​。您是否可以为测试用例创建一个基类,在那里定义setup然后记得在测试中需要覆盖它时调用super

编辑1

对不起,我错过了关于需要特别加入断言的观点。我意识到这与你的环境不匹配,但是使用rails 3和ruby 1.9,我已经得到了以下内容:

测试/ 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

运行基本assert true样式测试会产生:

(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

有用的是,我在rails和各种ruby库中看到的所有断言最终都以某种方式调用assert