从 WWW 浏览器自动检测区域设置并使用 Cucumber 进行测试

发布于 2024-08-15 11:40:32 字数 1744 浏览 1 评论 0原文

我用 Cucumber 测试了我的应用程序,在我在 application_controller.rb 中添加 WWW 浏览器的区域设置自动检测之前它就工作了:

  before_filter :set_locale

  private

    def set_locale
      xxx = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
      if xxx.match /^(en|fr)$/
        I18n.locale = xxx
      else
        I18n.locale = 'en'
      end
    end

我有一个场景:

  Scenario: Successful sign up
    Given I am an anonymous user
    And I am on the home page
    When ...

当我运行 cucumber 时,我收到错误:

Given I am an anonymous user                   # features/step_definitions/user_steps.rb:7
And I am on the home page                      # features/step_definitions/webrat_steps.rb:6
  private method `scan' called for nil:NilClass (NoMethodError)
  C:/workspace/jeengle/app/controllers/application_controller.rb:33:in `set_locale'
  c:/worktools/ruby/lib/ruby/1.8/benchmark.rb:308:in `realtime'
  (eval):2:in `/^I am on (.+)$/'
  features/manage_users.feature:8:in `And I am on the home page'

I尝试在step_definitions文件夹中的before语句中执行此操作:

Before do
  request.env['HTTP_ACCEPT_LANGUAGE'] = "en"
end

但我遇到了另一个错误:

  undefined method `env' for nil:NilClass (NoMethodError)

有人知道如何初始化/模拟request.env['HTTP_ACCEPT_LANGUAGE']在黄瓜?


更新

当我重写 set_locale 方法时,

  xxx = request.env['HTTP_ACCEPT_LANGUAGE']    
  if xxx
    xxx = xxx.scan(/^[a-z]{2}/).first
    if xxx.match /^(en|ru)$/
      I18n.locale = xxx
  end
  else
    I18n.locale = 'en'
  end

Cucumber 测试通过了:这不是一个解决方案,但它有效。

I test my application with Cucumber and it worked before I've added auto-detection of locales from WWW browser in application_controller.rb:

  before_filter :set_locale

  private

    def set_locale
      xxx = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
      if xxx.match /^(en|fr)$/
        I18n.locale = xxx
      else
        I18n.locale = 'en'
      end
    end

I have a scenario:

  Scenario: Successful sign up
    Given I am an anonymous user
    And I am on the home page
    When ...

When I run cucumber, I get an error:

Given I am an anonymous user                   # features/step_definitions/user_steps.rb:7
And I am on the home page                      # features/step_definitions/webrat_steps.rb:6
  private method `scan' called for nil:NilClass (NoMethodError)
  C:/workspace/jeengle/app/controllers/application_controller.rb:33:in `set_locale'
  c:/worktools/ruby/lib/ruby/1.8/benchmark.rb:308:in `realtime'
  (eval):2:in `/^I am on (.+)$/'
  features/manage_users.feature:8:in `And I am on the home page'

I have tried to do it in before statement in step_definitions folder:

Before do
  request.env['HTTP_ACCEPT_LANGUAGE'] = "en"
end

but I've got another error:

  undefined method `env' for nil:NilClass (NoMethodError)

Does anybody know how to initialize/emulate request.env['HTTP_ACCEPT_LANGUAGE'] in Cucumber?


UPDATED

Cucumber test passed when I rewritted set_locale method:

  xxx = request.env['HTTP_ACCEPT_LANGUAGE']    
  if xxx
    xxx = xxx.scan(/^[a-z]{2}/).first
    if xxx.match /^(en|ru)$/
      I18n.locale = xxx
  end
  else
    I18n.locale = 'en'
  end

It's not a solution but it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很快妥协 2024-08-22 11:40:32

其实问题出在Webrat,而不是Cucumber。事件的顺序(大致)是

  • Cucumber 运行你的功能
  • 当到达“我在主页”步骤时,它调用 Webrat 向控制器发出请求
  • Webrat 构造一个请求并将其发送到控制器
  • 步骤失败,因为请求没有“Accept-Language”标头

显然 Webrat 在构建请求时没有添加此标头。然而,Webrat 为您提供了一个解决方法:“header”方法,使用该方法可以在请求期间设置任何标头。

因此,为了使这项工作正常进行,请添加一个放入标头的步骤,例如:

Given /^an Accept Language header$/ do
  header "Accept-Language", "en;en-us" # or whatever value you need for testing
end`

在访问页面之前运行此步骤,Webrat 不再感到困惑。

顺便说一句,我从 The Rspec Book 得到这个,它确实很好地解释了 BDD 的内容如何组合在一起。

Actually the problem lies in Webrat, not Cucumber. The sequence of events is (roughly)

  • Cucumber runs your feature
  • When the "I am on the home page" step is reached, it invokes Webrat to make the request to the controller
  • Webrat constructs a request and sends it to the controller
  • Step fails because the request does not have an "Accept-Language" header

Apparently Webrat does not add this header when it builds the request. However, Webrat gives you a workaround: the "header" method, with which any header can be set for the duration of the request.

So to make this work add a step that puts in the header, e.g.:

Given /^an Accept Language header$/ do
  header "Accept-Language", "en;en-us" # or whatever value you need for testing
end`

Run this step before you visit a page and Webrat is no longer confused.

btw I got this from The Rspec Book, which really does a good job explaining how this BDD stuff fits together.

暗恋未遂 2024-08-22 11:40:32

另一种方式是“相同,但不同”。您可以在步骤定义文件中添加 before 语句:

Before do
  header 'Accept-Language', 'en-US' 
end

这将在每个场景之前执行,并且它们保持清晰和干净。

Another way is "same-same, but different". You can add before statement in your step definition file:

Before do
  header 'Accept-Language', 'en-US' 
end

This will be executed before each scenario and they remain clear and clean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文