AWDWR focuses on unit tests for models with database tables. There''s no discussion for models w/o tables that I can see. First attempts kept complaining about data tables not existing. I figured out this seems to come from the :fixtures line. Comment that out, and the test runs. OK, but I do want to use test data, it''s just that it is not going to come from a database table. Is there some way to declare that the fixture file represents data that has nothing to do with database tables? Seems something like fixtures should be adaptable for simulating any data source. Searching for some blog topics on this hasn''t been productive. Certainly I can do my own setup that loads text files of sample data or pulls from a dummy web service as the case may be, but I just want to be sure that''s the intended solution. -- gw --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Greg, I had to create unit tests for a model that was not in the database. I will share what I did, but my model was pretty much acted as a single object so there was not a big data requirement. The unit test did not load any fixtures and so I just created new model objects as I needed them using a protected function like this (I was shown this technique in training from the BigNerd Ranch and I find it indispensable when writing tests now): def create_credit_card(options={}) CreditCard.new({ :type => ''VI'', :number => ''4111111111111111'', :expires => ''05/10'' }.merge(options)) end I created tests like normal an just instantiated objects with the data I needed. def test_should_require_type card = create_credit_card(:type => '''') assert !card.valid? end Does this make sense? I am not sure how well this would work if you need a lot of data. Good luck! Peer Greg Willits wrote:> AWDWR focuses on unit tests for models with database tables. There''s > no discussion for models w/o tables that I can see. > > First attempts kept complaining about data tables not existing. I > figured out this seems to come from the :fixtures line. Comment that > out, and the test runs. > > OK, but I do want to use test data, it''s just that it is not going to > come from a database table. Is there some way to declare that the > fixture file represents data that has nothing to do with database > tables? Seems something like fixtures should be adaptable for > simulating any data source. > > Searching for some blog topics on this hasn''t been productive. > > Certainly I can do my own setup that loads text files of sample data > or pulls from a dummy web service as the case may be, but I just want > to be sure that''s the intended solution. > > -- gw-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 9, 2007, at 5:36 PM, Peer Allan wrote:> ...I just created new model > objects as I needed them using a protected function like this (I was > shown this technique in training from the BigNerd Ranch and I find it > indispensable when writing tests now):Yeah that could make sense for some cases. This particular one would need flexible data (it''s a config file parser). So, I have no problem using setup, or simply loadding data to start with by any means, just seemd weird that fixtures is dedicated to data table models. Anyway, I''m having a lot more fundamental problem now in that I can''t seem to create instances of my model class w/in the test code. Simplified version of my model... class ConfigData attr_reader :configData def initialize @configData = '''' end def loadFile(fileName) if FileTest::exist?(fileName) fileLines = IO.readlines(fileName) return fileLines end end ...lots of other methods... end Even this simplified test complains... require File.dirname(__FILE__) + ''/../test_helper'' class ConfigData < Test::Unit::TestCase def test_loadFile cnfgData = ConfigData.new end end What I get is a complaint about `initialize` having a "wrong number of arguments (0 for 1)" -- but there are no arguments required. I''m obviously missing the relationship of something somewhere, but I''m totally stumped. -- gw --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 9, 2007, at 6:42 PM, Greg Willits wrote:> Anyway, I''m having a lot more fundamental problem now in that I can''t > seem to create instances of my model class w/in the test code. > > Simplified version of my model... > > class ConfigData > > attr_reader :configData > > def initialize > @configData = '''' > end > > def loadFile(fileName) > if FileTest::exist?(fileName) > fileLines = IO.readlines(fileName) > return fileLines > end > end > > ...lots of other methods... > end > > > Even this simplified test complains... > > require File.dirname(__FILE__) + ''/../test_helper'' > class ConfigData < Test::Unit::TestCase > def test_loadFile > cnfgData = ConfigData.new > end > end > > What I get is a complaint about `initialize` having a "wrong number > of arguments (0 for 1)" -- but there are no arguments required.After pulling nearly all my hair out (and I have a lot of it), I found the problem. Notice this line: class ConfigData < Test::Unit::TestCase should be class ConfigDataTest < Test::Unit::TestCase Aargh. -- gw --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You might want to look into Mocha. Mocks can often put a finer point on what you''re trying to test -- particularly if it''s not db-related. Check out: http://blog.floehopper.org/articles/tag/unit. Consider this: class ConfigDataTest < Test::Unit::TestCase def LoadFileShouldOnlyReadFilesThatExist FileTest.stub!(:exist?).and_return(false) @config = ConfigData.new assert what_you_expect_when_the_file_doesnt_exist, @config.loadFile(''abc.txt'') end def LoadFileShouldReadFilesWhenTheyExist FileTest.stub!(:exist?).and_return(true) IO.should_receive(:readlines).once.and_return([''line1'', ''line2'', ''line3'']) @config = ConfigData.new assert_equal 3, @config.LoadFile(''test.txt'') end end I''m not sure this code actually works, but if you see what I''m driving at, you can test a lot of different scenarios with not a lot of code. Hope this helps. On Nov 9, 2007, at 8:07 PM, Greg Willits wrote:> > On Nov 9, 2007, at 6:42 PM, Greg Willits wrote: >> Anyway, I''m having a lot more fundamental problem now in that I can''t >> seem to create instances of my model class w/in the test code. >> >> Simplified version of my model... >> >> class ConfigData >> >> attr_reader :configData >> >> def initialize >> @configData = '''' >> end >> >> def loadFile(fileName) >> if FileTest::exist?(fileName) >> fileLines = IO.readlines(fileName) >> return fileLines >> end >> end >> >> ...lots of other methods... >> end >> >> >> Even this simplified test complains... >> >> require File.dirname(__FILE__) + ''/../test_helper'' >> class ConfigData < Test::Unit::TestCase >> def test_loadFile >> cnfgData = ConfigData.new >> end >> end >> >> What I get is a complaint about `initialize` having a "wrong number >> of arguments (0 for 1)" -- but there are no arguments required. > > > After pulling nearly all my hair out (and I have a lot of it), I > found the problem. > > Notice this line: > class ConfigData < Test::Unit::TestCase > should be > class ConfigDataTest < Test::Unit::TestCase > > Aargh. > > -- gw > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cool, I''ll check it out. When I created the Unit test framework for Lasso I included a lot of explicit assertions that made it a lot clearer what was being tested for as well. (www.l-unit.org) -- gw On Nov 9, 2007, at 9:05 PM, s.ross wrote:> > You might want to look into Mocha. Mocks can often put a finer point > on what you''re trying to test -- particularly if it''s not db-related. > Check out: http://blog.floehopper.org/articles/tag/unit. > > Consider this: > > class ConfigDataTest < Test::Unit::TestCase > def LoadFileShouldOnlyReadFilesThatExist > FileTest.stub!(:exist?).and_return(false) > @config = ConfigData.new > assert what_you_expect_when_the_file_doesnt_exist, > @config.loadFile(''abc.txt'') > end > > def LoadFileShouldReadFilesWhenTheyExist > FileTest.stub!(:exist?).and_return(true) > IO.should_receive(:readlines).once.and_return([''line1'', ''line2'', > ''line3'']) > @config = ConfigData.new > assert_equal 3, @config.LoadFile(''test.txt'') > end > end > > I''m not sure this code actually works, but if you see what I''m > driving at, you can test a lot of different scenarios with not a lot > of code. > > Hope this helps. > > > On Nov 9, 2007, at 8:07 PM, Greg Willits wrote: > >> >> On Nov 9, 2007, at 6:42 PM, Greg Willits wrote: >>> Anyway, I''m having a lot more fundamental problem now in that I >>> can''t >>> seem to create instances of my model class w/in the test code. >>> >>> Simplified version of my model... >>> >>> class ConfigData >>> >>> attr_reader :configData >>> >>> def initialize >>> @configData = '''' >>> end >>> >>> def loadFile(fileName) >>> if FileTest::exist?(fileName) >>> fileLines = IO.readlines(fileName) >>> return fileLines >>> end >>> end >>> >>> ...lots of other methods... >>> end >>> >>> >>> Even this simplified test complains... >>> >>> require File.dirname(__FILE__) + ''/../test_helper'' >>> class ConfigData < Test::Unit::TestCase >>> def test_loadFile >>> cnfgData = ConfigData.new >>> end >>> end >>> >>> What I get is a complaint about `initialize` having a "wrong number >>> of arguments (0 for 1)" -- but there are no arguments required. >> >> >> After pulling nearly all my hair out (and I have a lot of it), I >> found the problem. >> >> Notice this line: >> class ConfigData < Test::Unit::TestCase >> should be >> class ConfigDataTest < Test::Unit::TestCase >> >> Aargh. >> >> -- gw >> >> >> >> >>> > > > >-- gw --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
D''Oh. The assert in the second test case should have been: assert_equal 3, @config.LoadFile(''test.txt'').size On Nov 9, 2007, at 9:16 PM, Greg Willits wrote:> > Cool, I''ll check it out. > > When I created the Unit test framework for Lasso I included a lot of > explicit assertions that made it a lot clearer what was being tested > for as well. (www.l-unit.org) > > -- gw > > > > On Nov 9, 2007, at 9:05 PM, s.ross wrote: > >> >> You might want to look into Mocha. Mocks can often put a finer point >> on what you''re trying to test -- particularly if it''s not db-related. >> Check out: http://blog.floehopper.org/articles/tag/unit. >> >> Consider this: >> >> class ConfigDataTest < Test::Unit::TestCase >> def LoadFileShouldOnlyReadFilesThatExist >> FileTest.stub!(:exist?).and_return(false) >> @config = ConfigData.new >> assert what_you_expect_when_the_file_doesnt_exist, >> @config.loadFile(''abc.txt'') >> end >> >> def LoadFileShouldReadFilesWhenTheyExist >> FileTest.stub!(:exist?).and_return(true) >> IO.should_receive(:readlines).once.and_return([''line1'', ''line2'', >> ''line3'']) >> @config = ConfigData.new >> assert_equal 3, @config.LoadFile(''test.txt'') >> end >> end >> >> I''m not sure this code actually works, but if you see what I''m >> driving at, you can test a lot of different scenarios with not a lot >> of code. >> >> Hope this helps. >> >> >> On Nov 9, 2007, at 8:07 PM, Greg Willits wrote: >> >>> >>> On Nov 9, 2007, at 6:42 PM, Greg Willits wrote: >>>> Anyway, I''m having a lot more fundamental problem now in that I >>>> can''t >>>> seem to create instances of my model class w/in the test code. >>>> >>>> Simplified version of my model... >>>> >>>> class ConfigData >>>> >>>> attr_reader :configData >>>> >>>> def initialize >>>> @configData = '''' >>>> end >>>> >>>> def loadFile(fileName) >>>> if FileTest::exist?(fileName) >>>> fileLines = IO.readlines(fileName) >>>> return fileLines >>>> end >>>> end >>>> >>>> ...lots of other methods... >>>> end >>>> >>>> >>>> Even this simplified test complains... >>>> >>>> require File.dirname(__FILE__) + ''/../test_helper'' >>>> class ConfigData < Test::Unit::TestCase >>>> def test_loadFile >>>> cnfgData = ConfigData.new >>>> end >>>> end >>>> >>>> What I get is a complaint about `initialize` having a "wrong number >>>> of arguments (0 for 1)" -- but there are no arguments required. >>> >>> >>> After pulling nearly all my hair out (and I have a lot of it), I >>> found the problem. >>> >>> Notice this line: >>> class ConfigData < Test::Unit::TestCase >>> should be >>> class ConfigDataTest < Test::Unit::TestCase >>> >>> Aargh. >>> >>> -- gw >>> >>> >>> >>> >>>> >> >> >>> > > > > -- gw > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---