i18n 重定向破坏了我的测试
我有一个大型应用程序,通过 rspec 进行了一千多次测试。
我们只是选择重定向任何页面,例如:
/
/foo
/foo/4/bar/34
...
TO:
/en
/en/foo
/fr/foo/4/bar/34
....
所以我在 application.rb 中做了一个 before 过滤器,如下所示:
if params[:locale].blank?
headers["Status"] = "301 Moved Permanently"
redirect_to request.env['REQUEST_URI'].sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{I18n.locale}#{$2}" }
end
它工作得很好,但是......它破坏了我的很多测试,例如:
it "should return 404" do
Video.should_receive(:failed_encodings).and_return([])
get :last_failed_encoding
response.status.should == "404 Not Found"
end
要修复此测试,我应该做:
get :last_failed_encoding, :locale => "en"
但是...说实话,我不想一一修复我的所有测试...
我尝试将语言环境设为这样的默认参数:
class ActionController::TestCase
alias_method(:old_get, :get) unless method_defined?(:old_get)
def get(path, parameters = {}, headers = nil)
parameters.merge({:locale => "fr"}) if parameters[:locale].blank?
old_get(path, parameters, headers)
end
end
...但无法使这项工作... 有什么想法吗?
I have a big application covered by more than a thousand tests via rspec.
We just made the choice to redirect any page like :
/
/foo
/foo/4/bar/34
...
TO :
/en
/en/foo
/fr/foo/4/bar/34
....
So I made a before filter in application.rb like so :
if params[:locale].blank?
headers["Status"] = "301 Moved Permanently"
redirect_to request.env['REQUEST_URI'].sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{I18n.locale}#{$2}" }
end
It's working great but ... It's breaking a lot of my tests, ex :
it "should return 404" do
Video.should_receive(:failed_encodings).and_return([])
get :last_failed_encoding
response.status.should == "404 Not Found"
end
To fix this test, I should do :
get :last_failed_encoding, :locale => "en"
But ... seriously I don't want to fix all my test one by one ...
I tried to make the locale a default parameter like this :
class ActionController::TestCase
alias_method(:old_get, :get) unless method_defined?(:old_get)
def get(path, parameters = {}, headers = nil)
parameters.merge({:locale => "fr"}) if parameters[:locale].blank?
old_get(path, parameters, headers)
end
end
... but couldnt make this work ...
Any idea ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果之前没有定义这个区域设置,为什么不定义它呢?
在您的 applicationController 中:
在您的应用程序具有本地定义和未来链接之后可能会很好。
Why don't define this locale if not before ?
In your applicationController :
After you application has the local define and futur link could be good.