1

I suppose this is agnostic to what platform you are building your application in. For arguments sake, I am using Ruby on Rails. Here's the problem. I have two models which are tightly coupled, and by tightly coupled I mean they are in a has_many relationship. Hypothetically, let's say the two models are the following:

  • User model
  • Skill model

A user has many skills. In this particular application, the Skill can only belong to one user. Once the skill has reached a certain level of mastery, I would like to increase the user's achievements attribute by one. Very straight forward problem to hash out in as far as an implementation goes.

My question is, where does the testing for this logic fit best? Does it go into user_test.rb? skill_test.rb? My initial thought is that since it is the individual skill that will update the counter for the user, all testing should go into the skill test. My other thought is, who really cares as long as the test is there in the first place? Is there a right or wrong here?

randombits
  • 47,058
  • 76
  • 251
  • 433

2 Answers2

0

I would put it in the User model myself, because the skills do belong to the user and so does the achievement attribute, and you're testing user functionality.

Another way to think of it is, if this were a desktop application with no database, how would you rather create the classes representing users and skills? Would you want the user to have a list/set of skills, or the skill to have a user reference? I would say the former is the more intuitive design.

Since ORM technologies like ActiveRecord are designed to allow you to represent relational designs in an object-oriented format, it is perfectly legitimate to write tests as if the objects were pure object types designed the way you would ordinarily want to design them.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • One could argue those that if the functionality that adds to the achievement attribute in the Skill model is not working correctly, the error is in the Skill class, not in the User class.. – randombits Oct 21 '11 at 19:04
  • Just depends on how you look at it. I feel it's more natural to put it in the User class. The good news is you can go either way. – Platinum Azure Oct 21 '11 at 19:07
0

I think what happens here is that you don't need a Unit Test, but an Acceptance Test.

There is a question asking about the difference here.

Community
  • 1
  • 1
kikito
  • 51,734
  • 32
  • 149
  • 189