升级RSpec 2到3后RspecJunitFormatter报错无法运行测试
RSpec 升级到3.x后CircleCI测试报错:can't convert nil into Float (TypeError)
问题描述
将RSpec从2.99版本升级至3.x版本后,在CircleCI执行测试时触发类型转换错误,所有测试无法运行。测试在RSpec 2.99环境下可正常执行,但在3.0及以上版本中失败,报错信息为:rspec_junit_formatter.rb:73:in '%': can't convert nil into Float (TypeError)
报错堆栈信息
bundler: failed to load command: rspec (/home/circleci/test-application/vendor/bundle/ruby/2.4.0/bin/rspec) Coverage report generated for RSpec to /home/circleci/test-application/coverage. 5908 / 17112 LOC (34.53%) covered. /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter.rb:73:in `%': can't convert nil into Float (TypeError) from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter.rb:73:in `xml_dump_example' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter.rb:46:in `block in xml_dump_examples' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter.rb:39:in `each' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter.rb:39:in `xml_dump_examples' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter.rb:34:in `xml_dump' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter/rspec3.rb:21:in `block in dump_summary' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter/rspec3.rb:111:in `swap_rspec_configuration' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter/rspec3.rb:128:in `without_color' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec_junit_formatter-0.5.1/lib/rspec_junit_formatter/rspec3.rb:21:in `dump_summary' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/reporter.rb:137:in `block in notify' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/reporter.rb:136:in `each' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/reporter.rb:136:in `notify' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/reporter.rb:118:in `finish' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/reporter.rb:56:in `ensure in report' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/reporter.rb:56:in `report' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:108:in `run_specs' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:86:in `run' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:38:in `invoke' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/bin/rspec:25:in `load' from /home/circleci/test-application/vendor/bundle/ruby/2.4.0/bin/rspec:25:in `<top (required)>' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/cli/exec.rb:58:in `load' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/cli/exec.rb:58:in `kernel_load' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/cli/exec.rb:23:in `run' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/cli.rb:483:in `exec' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/cli.rb:31:in `dispatch' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/cli.rb:25:in `start' from /home/circleci/.rubygems/gems/bundler-2.3.12/exe/bundle:48:in `block in <top (required)>' from /home/circleci/.rubygems/gems/bundler-2.3.12/lib/bundler/friendly_errors.rb:103:in `with_friendly_errors' from /home/circleci/.rubygems/gems/bundler-2.3.12/exe/bundle:36:in `<top (required)>' from /home/circleci/.rubygems/bin/bundle:25:in `load' from /home/circleci/.rubygems/bin/bundle:25:in `<main>' Exited with code exit status 1
CircleCI配置
- run: name: Run tests command: | mkdir ~/test_results circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings> /tmp/tests-to-run bundle exec rspec $(cat /tmp/tests-to-run) --format documentation --profile --format RspecJunitFormatter -o ~/test_results/rspec.xml
使用版本信息
- Ruby 2.4
- RSpec 3.0
- rspec_junit_formatter 0.5.1
解决方案
问题根源
rspec_junit_formatter 0.5.1版本与RSpec 3.x的API不兼容,在获取测试用例运行时间时返回nil,导致生成JUnit XML报告时触发nil转Float的类型转换错误。
修复步骤
升级rspec_junit_formatter到兼容版本
修改Gemfile中的依赖声明:gem 'rspec_junit_formatter', '>= 0.6.0'执行命令更新依赖:
bundle update rspec_junit_formatter验证测试运行
重新触发CircleCI构建,确认错误消失,JUnit XML报告正常生成。可选:升级RSpec到更高版本
若项目允许,建议将RSpec升级到3.1及以上版本,进一步提升兼容性和测试框架的稳定性。
内容的提问来源于stack exchange,提问作者Vishal Jain
相关产品推荐
相关产品推荐

