RSpec & Rails: Stub @virtual_path for translation helper to check application helper

I have an assistant page_title_defaultin ApplicationHelper:

def page_title_default(options = {})
  t '.title', options
end

Now I want to test it as follows:

describe '#page_title' do
  subject { page_title }

  it { ... }
end

end

This results in the following error:

Cannot use t(".title") shortcut because path is not available

According to this post, it should be possible to drown out the variable @virtual_pathas follows:

helper.instance_variable_set(:@virtual_path, "admin.path.form")

But this does not help: although I can drown it out and then call something like helper.t '.something'directly in the test, this does not work for the translation helper, which is used in the page_title_defaultmethod (which still has @virtual_pathit set to nil). So this is not the same instance of the translation assistant. But how can I find a method page_title_default?

0
1

- :

RSpec.describe PageHelper, :type => :helper do
  describe "#page_title_default" do
    before do
      allow(helper).to receive(:t).with(".title", {}) { "Hello!" }
    end

    subject { helper.page_title_default }

    it { is_expected.to eq "Hello!" }
  end
end

"" , , helper "" , PageHelper - , ".title".

- , . ".title" ".default_title" , .

, ( ). , .

, !

+3

All Articles