Skip to content
This repository

html_escape cannot be called from helper tests #7330

Closed
ngan opened this Issue · 10 comments

5 participants

Ngan Pham Jeroen van Ingen Steve Klabnik Piotr Sarnacki Nimble Apps
Ngan Pham
ngan commented

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.

Jeroen van Ingen

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/

Steve Klabnik
Collaborator

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.

Piotr Sarnacki
Collaborator

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

Steve Klabnik
Collaborator

That works too.

Ngan Pham
ngan commented

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?

Piotr Sarnacki
Collaborator

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.

Steve Klabnik
Collaborator

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 Pham
ngan commented

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...

Steve Klabnik
Collaborator

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

Nimble Apps

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.