2

我的模型上有一个 validates_uniqueness_of 验证:

#SwimMeetRelayEvent.rb
validates_uniqueness_of :event_number_digit, :scope => [:swim_meet_id, :event_number_alpha]
validates_uniqueness_of :event_number_alpha, :scope => [:swim_meet_id, :event_number_digit]

#blueprint.rb
  SwimMeetRelayEvent.blueprint do
    swim_meet           { SwimMeet.make }
    athlete_min_age     { rand(4) + 8 }
    athlete_max_age     { rand(4) + 12 }
    athlete_gender      { Sham.gender }
    distance            { ['25', '50', '100'].sort_by { rand }.first }
    stroke              { [6, 7].sort_by {rand}.first }
    event_number_digit  { rand(100) + 1}
    event_number_alpha  { ('A'..'Z').to_a.sort_by {rand}.first}
  end

我正在使用机械师,并且在我运行测试时上述验证失败

SwimMeetRelayEvent.make

我只是在我的测试中制作了 1 个“事件”,并尝试了 event_number_digit、event_number_alpha 的不同值,但没有运气。有任何想法吗?

# trace
  1) Error:
test_something(Manage::SwimRelayTeamsHelperTest):
ActiveRecord::RecordInvalid: Validation failed: Event number digit has already been taken, Event number alpha has already been taken, Event number Event Number is already take
n
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/activerecord-2.3.11/lib/active_record/validations.rb:1102:in `save_with_validation!'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/activerecord-2.3.11/lib/active_record/dirty.rb:87:in `save_with_dirty!'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/activerecord-2.3.11/lib/active_record/transactions.rb:200:in `block (2 levels) in save_with_transactions!'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/activerecord-2.3.11/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/activerecord-2.3.11/lib/active_record/transactions.rb:182:in `transaction'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/activerecord-2.3.11/lib/active_record/transactions.rb:200:in `block in save_with_transactions!'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/activerecord-2.3.11/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/activerecord-2.3.11/lib/active_record/transactions.rb:200:in `save_with_transactions!'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/machinist-1.0.6/lib/machinist/active_record.rb:55:in `make'
    /Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@swimtopia/gems/machinist_callbacks-0.2.0/lib/machinist_callbacks.rb:51:in `block (2 levels) in <top (required)>'
    test/unit/helpers/manage/swim_relay_teams_helper_test.rb:5:in `block in <class:SwimRelayTeamsHelperTest>'

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

Test run options: --seed 62766
rake aborted!
Command failed with status (1): [/Users/dwalseth/.rvm/rubies/ruby-1.9.2-p13...]
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:995:in `block in sh'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:1010:in `call'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:1010:in `sh'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:1094:in `sh'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:1029:in `ruby'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:1094:in `ruby'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake/testtask.rb:117:in `block (2 levels) in define'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:1112:in `verbose'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake/testtask.rb:102:in `block in define'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain'
/Users/dwalseth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/gems/rake-0.8.7/bin/rake:31:in `<top (required)>'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/bin/rake:19:in `load'
/Users/dwalseth/.rvm/gems/ruby-1.9.2-p136@global/bin/rake:19:in `<main>'
4

1 回答 1

1

我将首先验证测试数据库是否在测试运行之间被清除。你可以运行:

rake db:test:prepare

哪个(默认情况下)将删除并重新创建测试数据库以匹配开发数据库模式——此时应该是空白的。如果你在运行这个之后对一个新的数据库进行测试,你还会遇到问题吗?

它们的顺序是随机的是否很重要?您可以定义一个按顺序生成它们的假:

Sham.define do
  event_number_digit { |index| (index % 100) + 1 }
  event_number_alpha { |index| ('A'..'Z').to_a[index] }
end

然后在你的蓝图中使用 shams。这样,您可以保证在违反唯一性约束之前可以连续制作 26 个。当你随机生成时,你随时都有随机失败的风险。

于 2011-06-24T19:20:50.123 回答