我似乎在使用FactoryGirl创建Time对象时遇到了一些麻烦。当我这样做时:
factory :invite do |n|
fromtime Time.now + 1.weeks
totime Time.now + 1.weeks + 1.hour
end
工厂工作正常,但我希望有许多顺序的邀请。所以当我这样做时:
factory :invite do |n|
fromtime Time.now + n.weeks
totime Time.now + n.weeks + 1.hour
end
我收到此错误
/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/core_ext/time/calculations.rb:312:in `+': can't convert FactoryGirl::Declaration::Implicit into an exact number (TypeError)
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/core_ext/time/calculations.rb:312:in `plus_with_duration'
from /Users/jamesstrocel/rails/gameplaydate/spec/factories.rb:24:in `block (2 levels) in <top (required)>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in `instance_eval'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in `factory'
from /Users/jamesstrocel/rails/gameplaydate/spec/factories.rb:22:in `block in <top (required)>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `run'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:7:in `define'
from /Users/jamesstrocel/rails/gameplaydate/spec/factories.rb:1:in `<top (required)>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:16:in `block in find_definitions'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl_rails-4.1.0/lib/factory_girl_rails/railtie.rb:26:in `block in <class:Railtie>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:34:in `call'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:42:in `each'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/jamesstrocel/rails/gameplaydate/config/environment.rb:5:in `<top (required)>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:103:in `require'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
如何使用连续的Time对象获得多个邀请?
答案 0 :(得分:5)
尝试:
factory :invite do
sequence(:fromtime) { |n| Time.now + n.weeks }
sequence(:totime) { |n| Time.now + n.weeks + 1.hour }
end
希望这有帮助!