Test::单元测试在 OS X 上通过,在 CentOS 上出错

发布于 08-06 19:04 字数 2750 浏览 5 评论 0原文

我有一组针对 Rails 应用程序的 Test::Unit 测试。它是在 Ruby 1.8.6、Rails 2.3.4 下的 OS X 上开发的。

我也在使用thoughtbot-shoulda 2.10.2。

我使用的是标准 Rails 固定装置,而不是工厂。

我已将该项目检查到 CentOS Linux 5 工作站上,供另一位开发人员处理。他运行的是 Ruby 1.8.7。

(该应用程序在生产环境中的 CentOS Linux 5 上运行,并且运行良好。)

在我同事的 CentOS 开发机器上,所有单元测试都通过了。

然而,大多数(但不是全部)功能测试都会出错。我隔离了一项测试(从项目中删除所有其他测试)以缩小故障排除范围。

  context 'on DELETE to :destroy' do
    setup {
      delete(:destroy, { :id => addresses(:mary_publics_address).id }, stans_id)
    }
    should 'delete the address' do
      assert Address.find(:all, :conditions => {
        :id => addresses(:mary_publics_address).id
      } ).blank?
    end
    should 'delete the addresses phone numbers' do
      assert PhoneNumber.find(:all, :conditions => {
        :id => phone_numbers(:mary_publics_phone_number).id
      } ).blank?
    end
  end

我们得到的错误是......

[abc@abc contactdb]$ rake test:functionals --trace
(in /home/abc/projects/contactdb)

[ ... ]

/usr/local/ruby_187/bin/ruby -I"lib:test" "/home/abc/.gem/ruby/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/addresses_controller_test.rb" 
Loaded suite /home/abc/.gem/ruby/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.E
Finished in 0.426749 seconds.

  1) Error:
test: on DELETE to :destroy should delete the addresses phone numbers. (AddressesControllerTest):
ActiveRecord::RecordNotFound: Couldn't find Address with ID=1254595889
    /test/functional/addresses_controller_test.rb:107:in `__bind_1255114457_160068'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:369:in `call'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:369:in `run_current_setup_blocks'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:368:in `each'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:368:in `run_current_setup_blocks'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:350:in `test: on DELETE to :destroy should delete the addresses phone numbers. '

2 tests, 1 assertions, 0 failures, 1 errors
rake aborted!
Command failed with status (1): [/usr/local/ruby_187/bin/ruby -I"lib:test" ...]

我认为关键的谜团是为什么它找不到具有该 ID 的 Address

另一个因素是,当我注释掉这个块时,剩余的测试就会通过。

    should 'delete the addresses phone numbers' do
      assert PhoneNumber.find(:all, :conditions => {
        :id => phone_numbers(:mary_publics_phone_number).id
      } ).blank?
    end

有人以前见过这个吗?

故障排除建议?

I have a set of Test::Unit tests for a Rails application. It was developed on OS X under Ruby 1.8.6, Rails 2.3.4.

I'm also using thoughtbot-shoulda 2.10.2.

I'm using standard Rails fixtures, not factories.

I've checked out the project onto a CentOS Linux 5 workstation for another developer to work on. He's running Ruby 1.8.7.

(The app is running on CentOS Linux 5 in production, and it's working fine there.)

On my coworker's CentOS dev machine, all of the unit tests are passing.

However, most, but not all, of the functional tests are erroring out. I've isolated one test (removing all the others from the project) to narrow down the troubleshooting scope.

  context 'on DELETE to :destroy' do
    setup {
      delete(:destroy, { :id => addresses(:mary_publics_address).id }, stans_id)
    }
    should 'delete the address' do
      assert Address.find(:all, :conditions => {
        :id => addresses(:mary_publics_address).id
      } ).blank?
    end
    should 'delete the addresses phone numbers' do
      assert PhoneNumber.find(:all, :conditions => {
        :id => phone_numbers(:mary_publics_phone_number).id
      } ).blank?
    end
  end

The error we're getting is...

[abc@abc contactdb]$ rake test:functionals --trace
(in /home/abc/projects/contactdb)

[ ... ]

/usr/local/ruby_187/bin/ruby -I"lib:test" "/home/abc/.gem/ruby/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/addresses_controller_test.rb" 
Loaded suite /home/abc/.gem/ruby/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.E
Finished in 0.426749 seconds.

  1) Error:
test: on DELETE to :destroy should delete the addresses phone numbers. (AddressesControllerTest):
ActiveRecord::RecordNotFound: Couldn't find Address with ID=1254595889
    /test/functional/addresses_controller_test.rb:107:in `__bind_1255114457_160068'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:369:in `call'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:369:in `run_current_setup_blocks'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:368:in `each'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:368:in `run_current_setup_blocks'
    /usr/local/ruby_187/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:350:in `test: on DELETE to :destroy should delete the addresses phone numbers. '

2 tests, 1 assertions, 0 failures, 1 errors
rake aborted!
Command failed with status (1): [/usr/local/ruby_187/bin/ruby -I"lib:test" ...]

I think the key mystery is why it can't find the Address with that ID.

Another factor is that when I comment out this block, the remaining test passes.

    should 'delete the addresses phone numbers' do
      assert PhoneNumber.find(:all, :conditions => {
        :id => phone_numbers(:mary_publics_phone_number).id
      } ).blank?
    end

Anyone seen this before?

Troubleshooting suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟2024-08-13 19:04:48

关于 Ruby 中的哈希,需要认识到的一件事是它们不保留顺序。在根据内存中加载的代码更改哈希顺序之前,我遇到过问题 - 即使在某处添加 puts "foo" 也会使错误消失,因为我没有意识到顺序Hash 的值在代码深处的某个地方产生了影响。 (注意:Hash 在 1.9.1 中确实保留了排序,具体是因为类似的问题,如果我猜的话。)这与您所说的注释掉代码如何使其他代码通过的说法一致。由于大多数装置都是使用 YAML 作为 Hash 读取的,因此有理由认为这可能是一个原因。找到 Hash 排序的地方(即在 each 之类的地方)会产生影响,在您的情况下可能有意义,也可能没有意义。如果没有别的事,那就是要记住的事情。

您是否尝试过使用 Fixtures.identify(:mary_publics_phone_number) 而不是 phone_numbers(:mary_publics_phone_number).id ? (另请参阅:Fixtures 文档。)另一件事要记住:您可能不会有独特的灯具名称。我会检查是否有重复项,以防万一。我认识很多人只是复制和粘贴固定装置,因为他们不知道 YAML 提供默认值的能力。在此过程中,他们可能会忘记更改灯具的名称。示例:

DEFAULTS: &DEFAULTS
  created_on: <%= 3.weeks.ago.to_s(:db) %>

first:
  name: Smurf
  <<: *DEFAULTS

从 OS X 迁移到 Linux 时我遇到的另一个问题是 Ruby 版本的细微差别。 (即使两者都报告 1.8.6,请记住补丁级别很重要。)过去的情况是,Red Hat 版本的 Ruby 在垃圾收集器中存在内存泄漏,有时需要我们重新启动长时间运行的进程。 (在我们意识到发生了什么之前,它产生了一些很难发现的错误,因为它们在很长时间内不会发生。)由于 CentOS 与 Red Hat 相关(基本上与 RHEL 相同),我可以想象其他版本差异会导致问题。我知道 OS X 从来没有出现过我描述的内存泄漏问题,这使得缩小错误范围变得更加困难。至于 1.8.6 和 1.8.7 之间的差异,您必须参考更改日志。请注意,从源代码构建的 Ruby 版本和打包版本的行为可能有所不同——我认为内存泄漏问题是由打包 Ruby 的人引入的。

这些只是一些可能的原因。请报告您的发现!

One thing to realize about Hashes in Ruby is that they don't retain ordering. I've had problems before that change the hash ordering depending on what code is loaded in memory -- even adding a puts "foo" somewhere would make a bug go away because I didn't realize the ordering of the Hash made a difference somewhere deep in the code. (Note: Hash does retain ordering in 1.9.1, specificly because of problems like that, if I were to guess.) This is consistent with what you say about how commenting out code makes other code pass. Since most fixtures are read in using YAML as Hashes, it's reasonable to think this might be a cause. Finding someplace that Hash ordering (i.e. in something like each) makes a difference may or may not make sense in your case. If nothing else, it's something to keep in mind.

Have you tried using Fixtures.identify(:mary_publics_phone_number) instead of phone_numbers(:mary_publics_phone_number).id? (See also: Fixtures documentation.) Another thing to keep in mind: you may not have unique fixture names. I'd check for duplicates, just in case. I know a lot of people who just copy and paste fixtures because they don't know about YAML's ability to give default values. In the process, they might forget to change the fixture's name. Example:

DEFAULTS: &DEFAULTS
  created_on: <%= 3.weeks.ago.to_s(:db) %>

first:
  name: Smurf
  <<: *DEFAULTS

Another issue I've had when moving from OS X to Linux is subtle differences in the Ruby version. (Even if both report 1.8.6, keep in mind that the patchlevel matters.) It used to be the case that the Red Hat version of Ruby had a memory leak in the garbage collector that required us to restart long running processes on occasion. (Before we realized what was going on, it created some hard to find bugs as they wouldn't happen for a long time.) Since CentOS is related to Red Hat (basically the same as RHEL), I could imagine other version differences causing issues. I know OS X never had the memory leak problem I described, which made it even harder to narrow the bug down. As far as differences go between 1.8.6 and 1.8.7, you'll have to refer to the change logs. Be aware that the version of Ruby built from source and the packaged version could behave differently -- I think the memory leak problem was introduced by whoever packaged Ruby.

Those are only a few possible causes. Please report back with what you find!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文