【发布时间】:2015-01-04 17:31:59
【问题描述】:
我想测试视图以确保正确呈现错误消息。我的config.default_locale 是'fr'。所以,我希望我的视图能够从我的法语语言环境文件中找到正确的 Active Record 错误消息。
describe 'book/new.html.erb' do
let(:subject) { rendered }
before do
@book = Book.create #this generates errors on my model
render
end
it { should match 'some error message in French' }
end
此测试在单独运行或与其他规范/视图一起运行时通过。但是当我运行完整的测试套件时,视图会显示以下消息:translation missing: en.activerecord.errors.models.book.attributes.title.blank。
我不明白为什么它使用 en 语言环境呈现。我试图通过以下方式强制语言环境:
before do
allow(I18n).to receive(:locale).and_return(:fr)
allow(I18n).to receive(:default_locale).and_return(:fr)
end
和
before do
default_url_options[:locale] = 'fr'
end
有人有想法吗?
【问题讨论】:
-
了解您使用的 RSpec 和 Rails 版本会很有帮助。
标签: ruby-on-rails ruby testing rspec rspec-rails