# Share code between unit and functional tests?

**URL:** <https://www.ruby-forum.com/t/share-code-between-unit-and-functional-tests/177089>\
**Category:** Rails\
**Created:** [October 11, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/share-code-between-unit-and-functional-tests/177089 "2009-10-11T12:18:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/share-code-between-unit-and-functional-tests/177089/1 "2009-10-11T12:18:48Z")

</div>

Hi all

I have the following helper that I use both in the unit and functional  
tests for my model Page:

class PagesControllerTest \< ActionController::TestCase  
…  
private  
def valid\_attributes  
{ :short\_title =\> “Valid short title”,  
:body =\> “

# Valid title

Valid body

”,  
:parent\_id =\> nil}  
end  
end

Is there a convenient way to remove this duplication and source it out  
to a file that is loaded by both the unit and the functional test files?

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/share-code-between-unit-and-functional-tests/177089/2 "2009-10-11T17:35:20Z")

</div>

Joshua M. wrote:

> Hi all
> 
> I have the following helper that I use both in the unit and functional  
> tests for my model Page:
> 
> class PagesControllerTest \< ActionController::TestCase  
> …  
> private  
> def valid\_attributes  
> { :short\_title =\> “Valid short title”,  
> :body =\> “
> 
> # Valid title
> 
> Valid body
> 
> ”,  
> :parent\_id =\> nil}  
> end  
> end
> 
> Is there a convenient way to remove this duplication and source it out  
> to a file that is loaded by both the unit and the functional test files?

With RSpec, you could use spec\_helper; with TestCase, I don’t know if  
there’s a standard location.

However, in either case, there’s a better solution to this particular  
issue. If you use Machinist, these attributes could go into a named  
blueprint.

> Thanks  
> Josh

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 9:18pm UTC](https://www.ruby-forum.com/t/share-code-between-unit-and-functional-tests/177089/3 "2009-10-11T21:18:39Z")

</div>

You can add it to the test\_helper.rb file and it will be included in  
all your default tests. Alternatively you could simply put it in a  
module (in a separate file in RAILS\_ROOT/test and then “include” it in  
test\_helper.rb or the individual test classes as you need.

On Oct 11, 10:35 am, Marnen Laibow-Koser \<rails-mailing-l…@andreas-
