ruby on rails-validates uniqueness_of测试失败,机械师



我有一个验证我的模型的validates_uniqueness_ofvalidation:

#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

我使用的是Machinist,每当我使用进行测试时,上述验证都会失败

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>'

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

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个。当你随机生成时,你在任何时候都有随机失败的风险。

相关内容

  • 没有找到相关文章

最新更新