使用 RSpec 和 Ruby on Rails 的国际字符 [英] International chars using RSpec with Ruby on Rails

查看:30
本文介绍了使用 RSpec 和 Ruby on Rails 的国际字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 RSpec,我在 RSpec github 存储库上复制了非常简单的测试,以确保一切按预期工作:

需要'spec_helper'描述主页"做它欢迎用户"做访问/产品"page.should have_content("欢迎")结尾结尾

当我将字符串更改为Olá"或Caçamba"之类的内容时,问题就开始了.任何带有特殊字符的字符串.当我这样做时,我收到以下错误:

无效的多字节字符 (US-ASCII) (SyntaxError)无效的多字节字符 (US-ASCII)语法错误,意外的 $end,期待 ')'page.should have_content("Olá")

关于如何修复它的任何想法?也许一些配置选项?非常感谢

解决方案

很可能您错过了神奇的评论 在文件顶部:

# 编码:UTF-8

如果没有这个指令,Ruby 会尝试使用默认的 US-ASCII 编码来解释你的文件,但会失败,因为这个字符集不包含像 áç 这样的符号.

这是关于默认源代码编码的博文詹姆斯·爱德华·格雷二世的红宝石.

I have just started using RSpec and I copied the very simple test on the RSpec github repo just to make sure things are working as expected:

require 'spec_helper'

describe 'Home Page' do
  it "Welcomes the user" do
    visit '/products'
    page.should have_content("Welcome")
  end
end

The problems begin when I change the string to something like "Olá" or "Caçamba". Any string with a special character. When I do that, I get the following error:

invalid multibyte char (US-ASCII) (SyntaxError)
invalid multibyte char (US-ASCII)
syntax error, unexpected $end, expecting ')'
page.should have_content("Olá")

Any ideas on how to fix it? Maybe some configuration option? Thanks a lot

解决方案

Most likely you're missing a magic comment at the top of your file:

# encoding: UTF-8

Without this directive Ruby tries to interpret your file with the default US-ASCII encoding and fails since this character set does not include symbols like á or ç.

Here's a blog post on default source encoding in Ruby by James Edward Gray II.

这篇关于使用 RSpec 和 Ruby on Rails 的国际字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆