# Testing with code injection

**URL:** <https://www.ruby-forum.com/t/testing-with-code-injection/123519>\
**Category:** Rails\
**Created:** [December 18, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/testing-with-code-injection/123519 "2007-12-18T12:02:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 18, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/testing-with-code-injection/123519/1 "2007-12-18T12:02:19Z")

</div>

Most of my unit tests are too long and repetative, so I’m trying to  
refactor them with some code injection.

What I want to do is something like in category\_test:

def setup  
@valid\_values = {:name =\> [“frank”, “bob, “vfdsnj543543”]}  
@invalid\_values = {:name =\> [”", nil, “some absurdely long name that  
won’t fit in the db”]}  
end

I’m struggling to find a way to inject the necessary methods to test  
all the possiblities. Doing anything with the base TestCase has  
undesireable results.

I’d like know what other people have done to achieve this sort of  
functionality?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 18, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/testing-with-code-injection/123519/2 "2007-12-18T12:29:36Z")

</div>

maybe rspec could help you, especially the stub and mock section

> **[RSpec: Behaviour Driven Development for Ruby](http://rspec.info/)**

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 18, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/testing-with-code-injection/123519/3 "2007-12-18T19:04:01Z")

</div>

Look over heckle ([http://seattlerb.rubyforge.org/heckle/](http://seattlerb.rubyforge.org/heckle/)).
