# Sharing unit tests across models

**URL:** <https://www.ruby-forum.com/t/sharing-unit-tests-across-models/181563>\
**Category:** Rails\
**Created:** [January 13, 2010, 10:15pm UTC](https://www.ruby-forum.com/t/sharing-unit-tests-across-models/181563 "2010-01-13T22:15:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 13, 2010, 10:15pm UTC](https://www.ruby-forum.com/t/sharing-unit-tests-across-models/181563/1 "2010-01-13T22:15:54Z")

</div>

Is there a way (or a best way) to share a test across models?

For example, if I have two models that validate presence of :name, can  
I write just one shared unit test to check it (and point the models to  
the shared test)?

Thanks!

Andrew

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 14, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/sharing-unit-tests-across-models/181563/2 "2010-01-14T16:06:52Z")

</div>

acreadinglist wrote:

> Is there a way (or a best way) to share a test across models?
> 
> For example, if I have two models that validate presence of :name, can  
> I write just one shared unit test to check it (and point the models to  
> the shared test)?

I understand the desire to be DRY everywhere, all the time, but this is  
one instance where I would not do it.

One unit test should be independent from all others. You should be able  
to copy a model from one application to another, along with it’s  
associated unit test file, and be able to run the unit tests without  
fail. If unit test files were sharing code between each other this  
wouldn’t work.

That’s just my view, but I don’t consider myself an authority on the  
subject.

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 14, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/sharing-unit-tests-across-models/181563/3 "2010-01-14T21:42:02Z")

</div>

Thanks. That is definitely a good/valid reason not to do it. Though  
I’d still be interested in hearing if there is a way to do so.

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 15, 2010, 9:25am UTC](https://www.ruby-forum.com/t/sharing-unit-tests-across-models/181563/4 "2010-01-15T09:25:17Z")

</div>

Marnen Laibow-Koser wrote:

> Robert W. wrote:
> 
> > acreadinglist wrote:
> > 
> > > Is there a way (or a best way) to share a test across models?
> > > 
> > > For example, if I have two models that validate presence of :name, can  
> > > I write just one shared unit test to check it (and point the models to  
> > > the shared test)?
> > 
> > I understand the desire to be DRY everywhere, all the time, but this is  
> > one instance where I would not do it.
> > 
> > One unit test should be independent from all others. You should be able  
> > to copy a model from one application to another, along with it’s  
> > associated unit test file, and be able to run the unit tests without  
> > fail.
> 
> Why? If you need the _exact_ same model in two apps, it should probably  
> be in a plugin or a gem instead. Anyway, if you have a library of test  
> macros, there’s no reason that you couldn’t include it in both places.

I suppose. I have just been under the impression, from what I’ve studied  
so far, that unit tests should be isolated so that they don’t have  
adverse affects outside of their context. As I mentioned I don’t  
consider myself an expert on the subject. I find that providing my  
opinions and getting feedback from the community helps me understand  
things more clearly.

> If unit test files were sharing code between each other this
> 
> > wouldn’t work.
> > 
> > That’s just my view, but I don’t consider myself an authority on the  
> > subject.
> 
> I sort of disagree. I’ve written the same RSpec code enough times that  
> I’m thinking of writing some custom matchers to DRY it up at least a  
> little bit (as Remarkable has already done). I think there’s some value  
> in being able to say  
> User.should\_have\_many :posts  
> User.should\_validate\_presence\_of :name

Writing custom matchers I certainly do agree with. It’s just the sharing  
of the actual unit tests that make me somewhat uneasy.

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 15, 2010, 3:10am UTC](https://www.ruby-forum.com/t/sharing-unit-tests-across-models/181563/5 "2010-01-15T03:10:13Z")

</div>

Robert W. wrote:

> acreadinglist wrote:
> 
> > Is there a way (or a best way) to share a test across models?
> > 
> > For example, if I have two models that validate presence of :name, can  
> > I write just one shared unit test to check it (and point the models to  
> > the shared test)?
> 
> I understand the desire to be DRY everywhere, all the time, but this is  
> one instance where I would not do it.
> 
> One unit test should be independent from all others. You should be able  
> to copy a model from one application to another, along with it’s  
> associated unit test file, and be able to run the unit tests without  
> fail.

Why? If you need the _exact_ same model in two apps, it should probably  
be in a plugin or a gem instead. Anyway, if you have a library of test  
macros, there’s no reason that you couldn’t include it in both places.

If unit test files were sharing code between each other this

> wouldn’t work.
> 
> That’s just my view, but I don’t consider myself an authority on the  
> subject.

I sort of disagree. I’ve written the same RSpec code enough times that  
I’m thinking of writing some custom matchers to DRY it up at least a  
little bit (as Remarkable has already done). I think there’s some value  
in being able to say  
User.should\_have\_many :posts  
User.should\_validate\_presence\_of :name

## Best,

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