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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Jan 14, 7:06 am, Robert Walker <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. 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. > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote:> Robert Walker 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 :nameWriting custom matchers I certainly do agree with. It''s just the sharing of the actual unit tests that make me somewhat uneasy. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.