I have a handful of custom helper methods stored in helpers/custom_helpers.rb
. I’m trying to write some basic unit tests (Rspec) to cover these methods, but I’m having trouble calling them from within my tests. For example, consider this method, whose purpose is to keep the “thank-you” pages from being indexed by crawlers:
def smart_robots
if !!(current_page.path =~ /thanks/)
"noindex, nofollow"
else
"index, follow"
end
end
In my site layout, I call it like so:
= tag "meta", name: "robots", content: smart_robots.to_s
When the site is running, it works as expected. But if I call the method from within my tests, I get undefined local variable or method current_page
. I’ve included the CustomHelpers module in my spec, and I’ve declared current_page
as a test double. Here’s the test code:
describe "#smart_robots" do
context "when on 'thanks' page" do
it "returns 'noindex, nofollow'" do
current_page = double("Sitemap")
allow(current_page).to receive(:path).and_return("contact/thanks.html")
expect(smart_robots).to eq("noindex, nofollow")
end
end
end
It seems apparent that there is an environment difference going on here. But I’m not sure what I need to include/require/instantiate so that current_page
will be available. Any thoughts?
You can see all the code on my “testing” feature branch here: https://github.com/joshukraine/euroteamoutreach.org/tree/testing
#smart_robots
method def: https://github.com/joshukraine/euroteamoutreach.org/blob/testing/helpers/custom_helpers.rb#L28
#smart_robots
test: https://github.com/joshukraine/euroteamoutreach.org/blob/testing/spec/helpers/custom_helpers_spec.rb#L56
Thank you in advance for your help!