Sharing unit tests across models

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

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.

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.

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.

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
[email protected]