I am writing a Danish web app, and trying to verify email addresses. These can include the characters æ ø å among others. Here is my rspec code:
User.create(@attr.merge(:email => 'æøå@bætter.nåw.ørg'.to_s)).should be_开发者_如何学Govalid
And my validation in User.rb:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true, :format => { :with => email_regex, :message => 'must be valid email' }, :uniqueness => true
This completely messes up rspec / autotest, which fails horribly with this stack trace:
Exception encountered: # "æøå@bætter.nåw.ørg".to_s)... ...
^> backtrace: /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:inload' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
block in load' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:inload_dependency' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
load' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:inblock in load_spec_files' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in
map' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:inload_spec_files' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:18:in
run' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.6.4/lib/rspec/monkey/spork/test_framework/rspec.rb:5:inrun_tests' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/spork-0.9.0.rc3/lib/spork/run_strategy/forking.rb:13:in
block in run' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/spork-0.9.0.rc3/lib/spork/forker.rb:21:inblock in initialize' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/spork-0.9.0.rc3/lib/spork/forker.rb:18:in
fork' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/spork-0.9.0.rc3/lib/spork/forker.rb:18:ininitialize' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/spork-0.9.0.rc3/lib/spork/run_strategy/forking.rb:9:in
new' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/spork-0.9.0.rc3/lib/spork/run_strategy/forking.rb:9:inrun' /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/spork-0.9.0.rc3/lib/spork/server.rb:48:in
run' /Users/houen/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/drb/drb.rb:1558:inperform_without_block' /Users/houen/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/drb/drb.rb:1518:in
perform' /Users/houen/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/drb/drb.rb:1592:inblock (2 levels) in main_loop' /Users/houen/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/drb/drb.rb:1588:in
loop' /Users/houen/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/drb/drb.rb:1588:inblock in main_loop' Unhandled exception: [#<ArgumentError: invalid byte sequence in US-ASCII>] /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:842:in
block in ': undefined methodbacktrace' for [#<ArgumentError: invalid byte sequence in US-ASCII>]:Array (NoMethodError) from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:828:in
[]' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:828:inblock in hook' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:828:in
each' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:828:inany?' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:828:in
hook' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:344:inrescue in run' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:320:in
run' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/lib/autotest.rb:241:inrun' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/gems/ZenTest-4.5.0/bin/autotest:6:in
' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/bin/autotest:19:inload' from /Users/houen/.rvm/gems/ruby-1.9.2-p180/bin/autotest:19:in
' from -e:1:inload' from -e:1:in
'Process finished with exit code 1
Any suggestions as to how I solve this?
Maybe add this to the top of your test file (the one with special characters):
# encoding: UTF-8
精彩评论