Testing helper in rspec with lazy I18n search

Consider this example. I have a product model that has discount_percentage. And we support multiple locales. In the view, we do not want the i18n material to ruin the view. Therefore, we help to read better and, possibly, reuse it in other views: render_product_discount(code see below), which will show the discount status of this product. And we use the i18n lazy search function throughout the application. But when we want to check this helper method, we get an error:

# RuntimeError:
# Cannot use t(".product_discount") shortcut because path is not available

because for the translator does not exist pathto extend the lazy translation key.

Expected Result: This product has a 20% discount.

Assistant Name: render_product_discount

def render_product_discount
  t('.product_discount', discount_percentage: product.discount_percentage)
end

# es.yml
es:
  products:
    show:
      product_discount: Este producto tiene un %{discount_percentage} descuento.

# en.yml
en:
  products:
    show:
      product_discount: This product has %{discount_percentage} discount.

How to do it? Thanks in advance.

+3
2

:

helper.stub(:t).with('.product_discount', discount_percentage: product.discount_percentage) { "This product has #{product.discount_percentage}% discount." }

:

expect(helper.render_product_discount).to eq("This product has #{product.discount_percentage}% discount.")

Edit
SebastianG, @virtual_path , , , , .

+1

, , .

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

t('.word') admin.path.form.word.

+11

All Articles