【问题标题】:FactoryGirl - Validation failed: Time zone is not included in the listFactoryGirl - 验证失败:时区未包含在列表中
【发布时间】:2012-09-11 00:34:07
【问题描述】:

我正在使用 Rspec、FactoryGirl 和 Capybara。使用 ActiveSupport::TimeZone.us_zones 时。我为我的requests/users_spec 运行测试,它甚至没有通过测试,因为它在工厂有问题。这是错误:

 Failure/Error: make_user_and_login
 ActiveRecord::RecordInvalid:
   Validation failed: Time zone is not included in the list
 # ./spec/support/user_macros.rb:3:in `make_user_and_login'
 # ./spec/requests/users_spec.rb:7:in `block (3 levels) in <top (required)>'

这是我的设置:

app/model/user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :remember_me, :username, :time_zone

  validates_presence_of :username
  validates_presence_of :time_zone
  validates_format_of :username, :with => /^(?=(.*[a-zA-Z]){3})[a-zA-Z0-9]+$/
  validates_uniqueness_of :email         
  validates_uniqueness_of :username, :case_sensitive => false
  validates_length_of :username, :within => 3..26
  validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.us_zones
end

spec/spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
  config.include(MailerMacros)
  config.include(UserMacros)
end

spec/factories.rb

FactoryGirl.define do
  factory :user do
    username   'user1'
    time_zone  '(GMT-05:00) Eastern Time (US & Canada)'
    email      'user@example.com'
    password   'testing'
  end
end

spec/support/user_macros.rb

module UserMacros
  def make_user_and_login
    user = FactoryGirl.create(:user)
    visit login_path
    page.should have_selector('title', :text => 'Login')
    fill_in('Email',    :with => user.email)
    fill_in('Password', :with => user.password)
    click_button('Login')
    page.should have_selector('title', :text => 'Home')
  end
end

当我为我的用户模型运行测试时,它通常会通过所有测试。为什么当它确实是时区时它不识别?

回答

在我的 User.rb 模型中我必须这样做:

validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.us_zones.map(&:name)

我的工厂:

factory :user do
  username   'user1'
  email      'user@example.com'
  time_zone  'Eastern Time (US & Canada)'
  password   'testing'
end

我的观点:

<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, {:prompt => "Select Your Time Zone *"} %>

如果您有来自 Capybara 的测试请求:

page.select('(GMT-10:00) Hawaii', :from => 'Time Zone')

祝你好运。

【问题讨论】:

  • 正如我在上一个问题中建议的那样——将时区数组转换为字符串数组:validates_inclusion_of :time_zone, :in =&gt; ActiveSupport::TimeZone.us_zones.map(&amp;:to_s)
  • @NickKugaevsky 是的,但那行不通。它返回错误:ArgumentError: Invalid Timezone: (GMT-05:00) Eastern Time (US &amp; Canada)
  • 真的很奇怪。刚刚检查了1.9.3-p194 :007 &gt; ActiveSupport::TimeZone.us_zones.map(&amp;:to_s).include?('(GMT-05:00) Eastern Time (US &amp; Canada)') =&gt; true
  • @NickKugaevsky 我正在使用带有1.9.3-p125 的Windows RailsInstaller,它也返回true。我的用户模型完全通过了。我将在我的编辑中向您展示。这不是使用ActiveSupport::TimeZone.us_zones.map(&amp;:to_s)。让我看看当我尝试时会发生什么。
  • @NickKugaevsky 是的,ActiveSupport::TimeZone.us_zones.map(&amp;:to_s) 也通过了。

标签: ruby-on-rails ruby-on-rails-3 capybara factory-bot rspec-rails


【解决方案1】:

在您的用户模型验证中将时区数组转换为字符串数组。

 validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.us_zones.map(&:to_s)

【讨论】:

  • 好的,这确实有效。与我的模型,所以我一定在某个地方走错了。我会看看我能对我的请求和水豚做些什么。我认为这是我可以解决的另一个问题。
  • 所以,验证不是完整的时区字符串,而只是它在模型中的名称。 8) ActiveSupport::TimeZone.us_zones.map(&amp;:name) 在视图中使用没有任何映射的时区数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多