Skip to content

html_escape cannot be called from helper tests #7330

Closed
ngan opened this Issue Aug 11, 2012 · 10 comments

5 participants

@ngan
ngan commented Aug 11, 2012

I can't seem to use ERBUtils methods like h or html_escape inside my helper specs.

For exampe....

assert_equal link_to(html_escape("<b>hello</b>")), link_to_custom("<b>hello</b>")

This will raise undefined method for html_escape.

@jeroeningen

Is this a bug, a feature request or just a question?

In the last case just the first think I think about it: ERB::Util.new.html_escape
I know it's pretty ugly, but this is a better place to ask it: https://www.ruby-forum.com/

@steveklabnik
Ruby on Rails member

Yes, this isn't really a bug; those helpers aren't in scope.

I would suggest what @jeroeningen says: create a semi-global ERB::Util object somewhere, and then use it to get access to these methods.

@drogus
Ruby on Rails member
drogus commented Aug 11, 2012

You can just include ERB::Util in your test case

@steveklabnik
Ruby on Rails member

That works too.

@ngan
ngan commented Aug 11, 2012

Hey guys, thanks for the feedback. The workaround isn't the problem for me, it's not too difficult as you guys have stated.

My question would be why isn't it included into the scope?

@drogus
Ruby on Rails member
drogus commented Aug 11, 2012

That's a good question. Before answering I tried to include ERB::Util in TestCase and some other test failed, so I didn't want to dig further. That fail looked like coincidence - meaning that it worked assuming that ERB::Util is not included, so there might be no reason.

Anyway, I think that it's rarely used in tests, so there is not much gain in including it, but I don't have anything against either.

@steveklabnik
Ruby on Rails member

Polluting namespaces is a bad idea. Many people don't need to use these in their tests, so no reason to introduce the burden on those who don't.

@ngan
ngan commented Aug 11, 2012

Quite strange. Shouldn't all helper methods be included or none? Strange to leave some out on the merits of assumption that many don't use it. I guess I gotta take it at face value...

I like how rspec puts helper methods inside a helper variable. Maybe rails should do something similar...

@steveklabnik
Ruby on Rails member

If I ruled rails (and/or cared enough to write a patch, heh), that's exactly how it'd work.

@nimbleapps

If it can help anyone, please note that this is a change between Rails 3.0 and Rails 3.1. In Rails 3.0, we can call html_escape in any subclass of ActionView::TestCase. Not in Rails 3.1.
There is a strange consequence: in Rails 3.1, if you need to test an helper that uses html_escape (which seems to be a pretty common case), you have to include ERB::Util in your test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.