Hi all I have the following helper that I use both in the unit and functional tests for my model Page: class PagesControllerTest < ActionController::TestCase ... private def valid_attributes { :short_title => "Valid short title", :body => "<h1>Valid title</h1><p>Valid body</p>", :parent_id => nil} end end Is there a convenient way to remove this duplication and source it out to a file that is loaded by both the unit and the functional test files? Thanks Josh -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-11 15:35 UTC
Re: Share code between unit and functional tests?
Joshua Muheim wrote:> Hi all > > I have the following helper that I use both in the unit and functional > tests for my model Page: > > class PagesControllerTest < ActionController::TestCase > ... > private > def valid_attributes > { :short_title => "Valid short title", > :body => "<h1>Valid title</h1><p>Valid body</p>", > :parent_id => nil} > end > end > > Is there a convenient way to remove this duplication and source it out > to a file that is loaded by both the unit and the functional test files?With RSpec, you could use spec_helper; with TestCase, I don''t know if there''s a standard location. However, in either case, there''s a better solution to this particular issue. If you use Machinist, these attributes could go into a named blueprint.> > Thanks > JoshBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
You can add it to the test_helper.rb file and it will be included in all your default tests. Alternatively you could simply put it in a module (in a separate file in RAILS_ROOT/test and then "include" it in test_helper.rb or the individual test classes as you need. On Oct 11, 10:35 am, Marnen Laibow-Koser <rails-mailing-l...@andreas- s.net> wrote:> Joshua Muheim wrote: > > Hi all > > > I have the following helper that I use both in the unit and functional > > tests for my model Page: > > > class PagesControllerTest < ActionController::TestCase > > ... > > private > > def valid_attributes > > { :short_title => "Valid short title", > > :body => "<h1>Valid title</h1><p>Valid body</p>", > > :parent_id => nil} > > end > > end > > > Is there a convenient way to remove this duplication and source it out > > to a file that is loaded by both the unit and the functional test files? > > With RSpec, you could use spec_helper; with TestCase, I don''t know if > there''s a standard location. > > However, in either case, there''s a better solution to this particular > issue. If you use Machinist, these attributes could go into a named > blueprint. > > > > > Thanks > > Josh > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.