# Testing helper methods in rspec

**URL:** <https://www.ruby-forum.com/t/testing-helper-methods-in-rspec/131696>\
**Category:** RSpec\
**Created:** [March 12, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/testing-helper-methods-in-rspec/131696 "2008-03-12T15:58:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 12, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/testing-helper-methods-in-rspec/131696/1 "2008-03-12T15:58:06Z")

</div>

This is perhaps a naive question, but what is the recommended manner of  
testing a helper method in rspec? I have created a simple string  
manipulation  
function and I want to write some tests for it. It it were a script  
then I  
would just add if **FILE** == $0 and add the tests below that but I do  
not  
feel that this is the best way to handle a rails helper.

The code (I said it was simple) :

module ApplicationHelper

# keycase strips leading spaces, squeezes out extra whitespace

# between words, downshifts all and then capitalizes the first

# character of each word.

# 

# This method prepares descriptive strings that are used

# as indices for lookups. To preserve common initialisms insert

# periods between letters. Thus:

# IBM =\> Ibm but I.B.M. =\> I.B.M.

# 

# Do not use ! methods as they return nil under certain circumstances

# 

def keycase(value)  
value = value.to\_s.strip.squeeze("  
").downcase.gsub(/\b\w/){$&.upcase}  
end

end

–  
\*\*\* E-Mail is NOT a SECURE channel \*\*\*  
James B. Byrne [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Harte & Lyne Limited [http://www.harte-lyne.ca](http://www.harte-lyne.ca)  
9 Brockley Drive vox: +1 905 561 1241  
Hamilton, Ontario fax: +1 905 561 0757  
Canada L8E 3C3

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 12, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/testing-helper-methods-in-rspec/131696/2 "2008-03-12T18:07:14Z")

</div>

On Wed, Mar 12, 2008 at 3:54 PM, James B. Byrne [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This is perhaps a naive question, but what is the recommended manner of  
> testing a helper method in rspec?

You just describe the helper module and call the helper method like  
you’d do in the views:

require File.dirname( **FILE** ) + ‘/../spec\_helper’

describe ApplicationHelper do  
describe “breadcrumbs” do  
describe “when breadcrumbs empty” do  
it “should show nothing” do  
breadcrumbs.should == “”  
end  
end  
end  
end

I have created a simple string manipulation

> # character of each word.
> 
> end
> 
> end

Something like this:

require File.dirname( **FILE** ) + ‘/../spec\_helper’

describe ApplicationHelper do  
describe “keycase” do  
describe “when no dots” do  
before(:each) do  
@value = “IBM”  
end

```
  it "should convert to lowercase" do
    keycase(@value).should == "Ibm"
  end
end

```

end  
end

And so on.

//jarkko

–  
Jarkko L.

> **[jlaine.net](https://jlaine.net)**

[http://odesign.fi](http://odesign.fi)
