【问题标题】:integration testing I18n for subdomain in Angular/Rails appAngular/Rails 应用程序中子域的集成测试 I18n
【发布时间】:2015-05-01 20:50:13
【问题描述】:

我正在尝试在 angular/rails 应用程序上使用 capybara 测试我的翻译。我在应用程序控制器中有一个 before 操作,它检查 url 中的子域并相应地设置语言环境。

为了在浏览器中手动测试,我必须修改我的 /hosts/etc 文件以包含我要测试的子域。该服务目前有效,但我想围绕它编写一个集成测试。

我发现了这个:http://www.chrisaitchison.com/2013/03/17/testing-subdomains-in-rails/,这似乎是一个可靠的解决方案,但它对我不起作用。每次我尝试运行测试时,I18n.locale 仍然设置为默认语言环境,而不是基于子域的语言环境。

有没有一种方法可以围绕这个编写集成测试,而不是让我期望页面有内容?我的测试目前看起来像:

context "no subdomain present", js: true do
    it "sets the locale to the default when no valid subdomain is present" do
      visit '/'
      expect(I18n.locale).to eq(I18n.default_locale)
    end
  end

  context "with a subdomain present", js: true do
    it "sets the locale based on the subdomain" do
      visit "hindi.127.0.0.1.xip.io:#{Capybara.server_port}/"
      sleep(2)
      expect(I18n.locale).to eq('hi')
    end
  end

顶部的测试当然通过了,但底部的测试没有。提前感谢,如果我忘记了什么,请告诉我,我会尽快添加。

【问题讨论】:

  • 你试过"http://hindi.127.0.0.1.xip.io:#{Capybara.server_port}/"吗?
  • 是的,我做到了,这导致了同样的错误。

标签: ruby-on-rails angularjs rspec internationalization capybara


【解决方案1】:

如果您不想测试页面内容,只需进行控制器测试:

describe SomeController do
  it "sets the locale to the default when no valid subdomain is present" do
    get :index
    expect(I18n.locale).to eq(I18n.default_locale)
  end
  it "sets locale from subdomain" do
    get :index, subdomain: 'hindu'
    expect(I18n.locale).to eq('hi')
  end
end

【讨论】:

  • 每当我必须进行数据库查询或检查我期望的一些内部导轨时,我都会尝试使用控制器规范。您希望从最终用户的角度保持您的所有功能都与测试有关(如果我做 x,我应该在屏幕上看到 x)。
  • 我无法进行控制器测试(据我所知),因为 Angular 负责应用程序的所有路由。所以得到 :index => Failure/Error: get :index ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"application"}
  • 这听起来不对 - Angular 可能会处理路由客户端,但 Rails 需要路由才能响应任何类型的请求。检查$ rake routes的输出
  • rake routes => 前缀动词 URI 模式控制器#Action guides GET /guides(.:format) guides#index guide GET /guides/:id(.:format) guides#show GET / application# layout GET /pages/*template(.:format) application#page errors POST /errors(.:format) application#error page GET /*path(.:format) application#layout root GET / application#layout
  • 好吧,让测试适应实际存在的路线之一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
相关资源
最近更新 更多