Nathan Long
Nathan Long

Reputation: 125892

Can I use a view helper in my request spec, or am I Doing It Wrong?

I'm going through a Rails app and updating all the flash messages to use our localization file. We have some request specs like this:

it "should alert on failure" do
  put "/lolcats/#{@lolcat.id}", lolcat: {caption: nil}
  response.body.should have_selector(
    "#flash-alert", text: "There was an error saving, please see below."
  )
end

I thought I'd rewrite that to expect whatever text is in the i18n file:

response.body.should have_selector(
  "#flash-alert", text: helper.t('flash.save_failed')
)

However, neither t nor helper.t are available here. I get this error:

undefined local variable or method `helper' for #<RSpec::Core::ExampleGroup...>

Can I access this view helper somehow, or am I Doing It Wrong?

Upvotes: 1

Views: 211

Answers (1)

Nash Bridges
Nash Bridges

Reputation: 2370

Since t in Rails views is a shortcut for I18n.t you can use latter in your spec.

Upvotes: 2

Related Questions