ruby – Rails测试显示0%通过时全部通过

前端之家收集整理的这篇文章主要介绍了ruby – Rails测试显示0%通过时全部通过前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的Rails应用程序通过了测试,但随后说0%通过了.它的Rails 3应用程序转换为3.1.
5 tests,11 assertions,0 failures,0 errors,0 pendings,0 omissions,0 notifications
0% passed

如果我生成一个新的Rails 3.1应用程序,它没有这个“通过”线.

1 tests,1 assertions,0 skips

两者都在1.9.2 p290下运行.

我试图删除测试单元,但这似乎并没有什么区别,没有其他任何突出的东西会导致它.我想有些东西正在触发使用不同的测试运行器.

这些是我的应用程序安装的宝石:

actionmailer (3.1.1)
actionpack (3.1.1)
activemodel (3.1.1)
activerecord (3.1.1)
activeresource (3.1.1)
activesupport (3.1.1)
ansi (1.3.0)
archive-tar-minitar (0.5.2)
arel (2.2.1)
bcrypt-ruby (3.0.1)
bootstrap-sass (1.3.0)
builder (3.0.0)
bundler (1.0.21)
coderay (0.9.8)
coffee-rails (3.1.1)
coffee-script (2.2.0)
coffee-script-source (1.1.2)
columnize (0.3.4)
devise (1.4.7)
devise_rpx_connectable (0.2.2)
enumerated_attribute (0.2.16)
erubis (2.7.0)
execjs (1.2.9)
haml (3.1.3)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.14)
json (1.6.1)
json_pure (1.6.1)
linecache19 (0.5.12)
mail (2.3.0)
Meta_programming (0.2.2)
method_source (0.6.6)
mime-types (1.16)
multi_json (1.0.3)
orm_adapter (0.0.5)
polyglot (0.3.2)
pry (0.9.6.2)
pry-doc (0.3.0)
rack (1.3.4)
rack-cache (1.1)
rack-mount (0.8.3)
rack-ssl (1.3.2)
rack-test (0.6.1)
rack-webconsole (0.1.2)
rails (3.1.1)
railties (3.1.1)
rake (0.9.2)
rdoc (3.10)
rpx_now (0.6.24)
ruby-debug-base19 (0.11.25)
ruby-debug19 (0.11.6)
ruby_core_source (0.1.5)
ruby_parser (2.0.6)
sass (3.1.10)
sass-rails (3.1.4)
sexp_processor (3.0.7)
slop (2.1.0)
sprockets (2.0.2)
sqlite3 (1.3.4)
test-unit (2.4.0)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
turn (0.8.2)
tzinfo (0.3.30)
uglifier (1.0.3)
warden (1.0.6)
yard (0.7.2)

这就是我使用默认的rails new’d app获得的

actionmailer (3.1.1)
actionpack (3.1.1)
activemodel (3.1.1)
activerecord (3.1.1)
activeresource (3.1.1)
activesupport (3.1.1)
ansi (1.3.0)
arel (2.2.1)
builder (3.0.0)
bundler (1.0.21)
coffee-rails (3.1.1)
coffee-script (2.2.0)
coffee-script-source (1.1.2)
erubis (2.7.0)
execjs (1.2.9)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.14)
json (1.6.1)
mail (2.3.0)
mime-types (1.16)
multi_json (1.0.3)
polyglot (0.3.2)
rack (1.3.4)
rack-cache (1.1)
rack-mount (0.8.3)
rack-ssl (1.3.2)
rack-test (0.6.1)
rails (3.1.1)
railties (3.1.1)
rake (0.9.2)
rdoc (3.10)
sass (3.1.10)
sass-rails (3.1.4)
sprockets (2.0.2)
sqlite3 (1.3.4)
test-unit (2.4.0)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
turn (0.8.2)
tzinfo (0.3.30)
uglifier (1.0.3)

编辑

我从应用程序中删除了所有内容并将其推送到github – https://github.com/kimptoc/zero-percent-bugette
当我只是跑“耙”时,我仍然得到0%的通过:

$rake
Loaded suite /Users/kimptoc/.rvm/gems/ruby-1.9.2-p290@p-viadropBox31/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started

Finished in 0.000897 seconds.

1 tests,0 notifications
0% passed

1114.83 tests/s,1114.83 assertions/s
Coverage report generated for /Users/kimptoc/.rvm/gems/ruby-1.9.2-p290@p-viadropBox31/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb  to    /Users/kimptoc/dev/ruby/fran/zero-percent/coverage. 4 / 4 LOC (100.0%) covered.

解决方法

如果您使用带有Rails的测试单元,那么您将无法执行任何操作.我相信它是Rails附带的activesupport gem中的一个错误.更详细:

通常,测试单元有一个方法add_pass,可以增加通过的测试计数.这称为run(…)方法(testcase.rb文件).但是,activesupport会使用自己的实现隐藏/覆盖此方法.它是文件setup_and_teardown.rb中的方法run(…)(方法的全名是ActiveSupport :: Testing :: SetupAndTeardown :: ForClassicTestUnit#run().

修改了这个方法如下:

def run(result)
      return if @method_name.to_s == "default_test"

      mocha_counter = retrieve_mocha_counter(result)
      yield(Test::Unit::TestCase::STARTED,name)
      @_result = result

      begin
        begin
          _run_setup_callbacks do
            setup
            __send__(@method_name)
            mocha_verify(mocha_counter) if mocha_counter
          end
        result.add_pass # XXXXXXXXXXXX My Addition to count passed tests XXXXXXXXXX
        rescue Mocha::ExpectationError => e
          add_failure(e.message,e.backtrace)
        rescue Test::Unit::AssertionFailedError => e
          add_failure(e.message,e.backtrace)
        rescue Exception => e
          raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
          add_error(e)
        ensure
          begin
            teardown
            _run_teardown_callbacks
          rescue Test::Unit::AssertionFailedError => e
            add_failure(e.message,e.backtrace)
          rescue Exception => e
            raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
            add_error(e)
          end
        end
      ensure
        mocha_teardown if mocha_counter
      end

      result.add_run
      yield(Test::Unit::TestCase::FINISHED,name)
    end

添加了不存在的result.add_pass,现在百分比应该没问题.

更新:为了帮助您集成此更改并自行测试/查看,您可以使用以下命令克隆我创建的要点:

git clone git://gist.github.com/1324512.git gist-1324512

这将创建一个名为active_support_test_unit_pass_count.rb的文件

将其移至test_helper.rb文件所在的测试文件夹,并在test_helper.rb文件中将其移至需要它,正好在ActiveSupport :: TestCase类的定义之前:

require 'active_support_test_unit_pass_count'

class ActiveSupport::TestCase

....

然后运行测试并查看百分比结果.对我来说……它运作正常.

原文链接:https://www.f2er.com/ruby/270878.html

猜你在找的Ruby相关文章