Wrong translation path in rspec view tests

Viewed 38

Hey guys I am running into a bit of trouble with a simple view test written using Rspec. The case is pretty simple. I want to render a page that should display a message asking the customer if he/she is new.

The page contains several subsections that contain translations. The problem is that Rspec puts a "test" in this translation path, so the correct translation is not found.

Rspec test:

require "rails_helper"

RSpec.describe 'checkout/start/index' do
  context "with no logged in customer" do
    it "should render the #index page and display the registration and login form" do
      render

      expect(rendered).to match(/Sie sind neu bei uns?/)
    end
  end
end

Response:

Randomized with seed 40265

checkout/start/index
  with no logged in customer
    calls #index and displays the registration and login form (FAILED - 1)

Failures:

  1) checkout/start/index with no logged in customer calls #index and displays the registration and login form
     Failure/Error: <%= t(".checkout_#{controller_name}_sub_headline") %>
     
     ActionView::Template::Error:
       translation missing: de.checkout.shared.sub_headline.checkout_test_sub_headline
     # /home/arthur/.rvm/gems/ruby-3.1.2/gems/i18n-1.12.0/lib/i18n.rb:394:in `handle_exception'
     # /home/arthur/.rvm/gems/ruby-3.1.2/gems/i18n-1.12.0/lib/i18n.rb:367:in `translate_key'
     # /home/arthur/.rvm/gems/ruby-3.1.2/gems/i18n-1.12.0/lib/i18n.rb:222:in `translate'
     # ./app/views/checkout/shared/_sub_headline.html.erb:6:in `_app_views_checkout_shared__sub_headline_html_erb__648894094841044844_23060'
     # ./app/views/checkout/start/index.html.erb:8:in `_app_views_checkout_start_index_html_erb__2185710400981636407_23000'
     # ./spec/views/checkout/start_spec.rb:10:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # I18n::MissingTranslationData:
     #   translation missing: de.checkout.shared.sub_headline.checkout_test_sub_headline
     #   /home/arthur/.rvm/gems/ruby-3.1.2/gems/i18n-1.12.0/lib/i18n.rb:394:in `handle_exception'

Top 1 slowest examples (0.03185 seconds, 6.4% of total time):
  checkout/start/index with no logged in customer calls #index and displays the registration and login form
    0.03185 seconds ./spec/views/checkout/start_spec.rb:9

Finished in 0.49579 seconds (files took 1.69 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/views/checkout/start_spec.rb:9 # checkout/start/index with no logged in customer calls #index and displays the registration and login form

Randomized with seed 40265

Is there any way to override this path for the test, or any other way to tell Rspec where to look for the correct translation?

Many thanks in advance

1 Answers

You have created a translation that's dependent upon the controller_name, which as you're seeing is different in your test environment because of the way Rails view tests work. If you need that in your code, then in your test I would stub it out so you're providing what your code expects. I'm not sure, but that's probably something like allow(helper).to receive(:controller_name) { 'your_controller_name' }

Related