Patrick J. Collins
2012-May-30 18:55 UTC
[rspec-users] alternate way to stub out I18n translations?
Hi,
I''ve run into a bit of a snag...
I have a method like this
class Thing
def bar
I18n.t("waka.waka", { :fozzy => "bear" })
end
end
And I was trying to make a test that did:
describe "#bar" do
it "translates like a mo fo" do
I18n.stubs(:t).with("waka.waka", { :fozzy => "bear"
}).returns "hooray!"
subject.bar.should == "hooray!"
end
end
However running this test gives me:
Mocha::ExpectationError:
unexpected invocation: I18n.t(:user_subject, {:scope => [:devise,
:mailer, :confirmation_instructions], :default => [:subject,
''Confirmation instructions'']}
...
Which is obviously because when I stub the t method, I am killing any
previous behavior.......... So, I am quite confused what to do here.
If I try to just be more generic and do:
describe "#bar" do
it "translates like a mo fo" do
I18n.stubs(:t).returns "hooray!"
subject.bar.should == "hooray!"
end
end
Then I get a whole different weird error:
Failure/Error: let(:notification) { new_notification(:notifiable =>
create_offer, :action => "made") }
ArgumentError:
wrong number of arguments (4 for 5)
...
But I don''t want to be generic like that anyway--- So I am wondering
what can I do to solve this?
Does any one know of a way to temporarily add an entry into my en.yml
locale dynamically? In other words, so that instead of stubbing I could
just do something like:
I18n.t("waka")[:waka] = "hooray!"
and then in code can still access
I18n.t("waka.waka").. But that doesn''t work, and I am not
sure how to
pull that off... Any help would be greatly appreciated.
Patrick J. Collins
http://collinatorstudios.com
Katrina Owen
2012-May-30 20:19 UTC
[rspec-users] alternate way to stub out I18n translations?
On Wed, May 30, 2012 at 8:55 PM, Patrick J. Collins <patrick at collinatorstudios.com> wrote:> Does any one know of a way to temporarily add an entry into my en.yml > locale dynamically?I usually do something like this in the before filter en = { :my => { :stuff => ''things'' } } I18n.backend.store_translations(:en, en) Cheers, Katrina