Reputation: 4867
Here's my folder structure:
root
- app
- helpers
- application_helper.rb
- test
- helpers
- application_helper_test.rb
Here's what application_helper.rb looks like:
module ApplicationHelper
def replace_links_with_urls(text)
text.gsub(%r{<a[^>]*? href=['"]([^'"]*)?['"].*?>(.*?)</a>}m, "\\2 ( \\1 )")
end
end
Here's what application_helper_test.rb looks like:
require File.expand_path("../../../app/helpers/application_helper", __FILE__)
describe ApplicationHelper do
describe "#replace_links_with_urls" do
it "does not replace non-links" do
ApplicationHelper::replace_links_with_urls("ABC <b>bold</b>").should == "ABC <b>bold</b>"
end
end
end
The error I get is:
NoMethodError: undefined method `replace_links_with_urls' for ApplicationHelper:Module
What am I doing wrong?
Upvotes: 0
Views: 27
Reputation: 2273
You need to declare your function using self, like this:
module ApplicationHelper
def self.replace_links_with_urls(text)
text.gsub(%r{<a[^>]*? href=['"]([^'"]*)?['"].*?>(.*?)</a>}m, "\\2 ( \\1 )")
end
end
Then you can call it using:
ApplicationHelper.replace_links_with_urls("asd")
Or declare it without using self, include the module using
include ApplicationHelper
And call the function
So your test will be:
require File.expand_path("../../../app/helpers/application_helper", __FILE__)
include ApplicationHelper
describe ApplicationHelper do
describe "#replace_links_with_urls" do
it "does not replace non-links" do
ApplicationHelper::replace_links_with_urls("ABC <b>bold</b>").should == "ABC <b>bold</b>"
end
end
end
Also, check that the code you posted the replace_links_with_urls function has two def, don't know if you have that same typo in your code or just here.
Upvotes: 1