ruby-on-rails - 使用 RSpec : setting locale based on first subdomain 进行测试

标签 ruby-on-rails ruby-on-rails-3 rspec rspec2 rspec-rails

我是 RSpec 的新手,我不知道如何测试以下内容:

在我的应用程序 Controller 中(在 Rails 3 应用程序中),我在一个 before 过滤器中设置了语言环境,就像这样

def set_locale
  I18n.locale = ["en", Setting.locale, get_locale_from_subdomain].compact.last
end

def get_locale_from_subdomain
  locale = request.subdomain.split('.').first
  return nil unless LOCALES.include? locale
  locale
end

所以基本上,'en.example.com' 和 'example.com' 将有一个“en”语言环境,而 'fr.example.com' 会将语言环境设置为“fr”。

我该如何测试?

最佳答案

我会做这样的事情:

I18n.available_locales.each do |locale|
  request.host = "#{locale}.example.com"
  get :index
  I18n.locale.should == locale
end

使用这里描述的匿名 Controller :

https://www.relishapp.com/rspec/rspec-rails/v/2-4/docs/controller-specs/anonymous-controller

另请参阅此问题:

Rails rspec set subdomain

关于ruby-on-rails - 使用 RSpec : setting locale based on first subdomain 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5710904/

相关文章:

ruby-on-rails - 使用 Rails 控制台截断表

ruby-on-rails - 如何使用 Rails 使用 CoffeeScript 进行开发

testing - 如何将参数发送到 FactoryGirl 特征?

ruby - 有没有更好的方法用 RSpec 测试这个 Ruby 类?

ruby-on-rails - Rails http 缓存尝试写入 tmp/cache。为什么?

ruby-on-rails - 如何防止 Rails Controller 生成器修改 config/routes.rb

ruby-on-rails - 在 Rails 3 中渲染到任意布局文件

ruby-on-rails - Rails 事件记录 : query for record by a datetime field?

ruby - Gem 在 irb 中加载但不在控制台中加载

ruby-on-rails - 如何用 capybara 填写日期时间字段?