从 WWW 浏览器自动检测区域设置并使用 Cucumber 进行测试
我用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其实问题出在Webrat,而不是Cucumber。事件的顺序(大致)是
显然 Webrat 在构建请求时没有添加此标头。然而,Webrat 为您提供了一个解决方法:“header”方法,使用该方法可以在请求期间设置任何标头。
因此,为了使这项工作正常进行,请添加一个放入标头的步骤,例如:
在访问页面之前运行此步骤,Webrat 不再感到困惑。
顺便说一句,我从 The Rspec Book 得到这个,它确实很好地解释了 BDD 的内容如何组合在一起。
Actually the problem lies in Webrat, not Cucumber. The sequence of events is (roughly)
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.:
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.
另一种方式是“相同,但不同”。您可以在步骤定义文件中添加 before 语句:
这将在每个场景之前执行,并且它们保持清晰和干净。
Another way is "same-same, but different". You can add before statement in your step definition file:
This will be executed before each scenario and they remain clear and clean.