[rspec-users] alternate way to stub out I18n translations?

Patrick J. Collins patrick at collinatorstudios.com
Wed May 30 18:55:28 UTC 2012


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" })

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!"

However running this test gives me:

       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!"

Then I get a whole different weird error:

     Failure/Error: let(:notification) { new_notification(:notifiable => create_offer, :action => "made") }
       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

More information about the rspec-users mailing list