1
votes

I'm using Rspec, FactoryGirl and Capybara. When using ActiveSupport::TimeZone.us_zones. I run my test for my requests/users_spec and it doesn't even hit the test as it has an issue at the Factory. Here is the error:

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

This is my setup:

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      '[email protected]'
    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

When I run test for my user model in general it passes all test. Why doesn't it recognize as a Time Zone when it really is?

ANSWER

In my User.rb model I had to do:

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

My Factory:

factory :user do
  username   'user1'
  email      '[email protected]'
  time_zone  'Eastern Time (US & Canada)'
  password   'testing'
end

My Views:

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

And if you have a test request from Capybara:

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

Good luck.

1
As I adviced you in previous question – convert array of timezones to array of strings: validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.us_zones.map(&:to_s)Nick Kugaevsky
@NickKugaevsky Yeah that didn't work though. It came back with the error: ArgumentError: Invalid Timezone: (GMT-05:00) Eastern Time (US & Canada)LearningRoR
It's really very strange. Just checked it out 1.9.3-p194 :007 > ActiveSupport::TimeZone.us_zones.map(&:to_s).include?('(GMT-05:00) Eastern Time (US & Canada)') => true Nick Kugaevsky
@NickKugaevsky I'm using the Windows RailsInstaller with 1.9.3-p125 and it returns true as well. My user model passes entirely. I will show you in my edit. This is not using ActiveSupport::TimeZone.us_zones.map(&:to_s). Let me see what happens when I try it.LearningRoR
@NickKugaevsky Yep, it passes as well with ActiveSupport::TimeZone.us_zones.map(&:to_s).LearningRoR

1 Answers

3
votes

Convert array of timezones to array of strings in your user model validations.

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