www.flickr.com
|
Sunday, November 19, 2006
Rails realities part 21 (crazy functional test problems)
Ever see a problem where your app appears to work fine and running a single functional test "-n test_foo" works as well, but if you run rake or the entire functional test class things blow up? Well that's what happened to me yesterday and
part of today.
It all started when I wanted to build a form based on the accessible attributes in one of my AR model classes, minus 3 attributes out of that array. So for example I have:
Which returns me all of the attributes I've specified in my model as being settable en masse...
I wanted to pull firstname, middlename, and lastname out of that array and build a form with the rest of the fields in that list. So I thought I'd be clever and instead of building it manually I'd remove those entries and iterate over the rest of the elements.
I was proud and patting myself on the back until I ran my tests. I started getting crazy errors that made no sense. After digging deeper my tests were failing because my model instances were missing first and lastnames. But because I made these changes on different days it didn't dawn on me right away what was happening coupled with the fact that the the app worked fine in development mode and tests would pass when run individually. Earlier today it finally clicked as to what the problem was and of course when I tested my theory, things were fixed. How's about cloning the array of accessible attributes since they're a class level collection that you're mutating there? Oops. Bad programmer. I know better but sometimes, well you just make mistakes.
After making that small change everything was fine again and I was able to take out my gobs of debugging code in AR :-)
part of today.
It all started when I wanted to build a form based on the accessible attributes in one of my AR model classes, minus 3 attributes out of that array. So for example I have:
ContactInfo.accessible_attributes
Which returns me all of the attributes I've specified in my model as being settable en masse...
attr_accessible :firstname, :middlename, :lastname, :organization, :work_phone, :title, :url, :email, :mobile_phone, :fax
I wanted to pull firstname, middlename, and lastname out of that array and build a form with the rest of the fields in that list. So I thought I'd be clever and instead of building it manually I'd remove those entries and iterate over the rest of the elements.
<%cols = ContactInfo.accessible_attributes
# remove the first 3 atts, first, middle, and last..
cols.delete_if {|col| cols.index(col) < 3}
cols.each do |col| %>
<dt><label for='contact_<%=col.to_s%>'><%=col.to_s.humanize%></label></dt>
<dd><%=text_field :contact, col, :size => 30%><br/></dd>
<%end%>
I was proud and patting myself on the back until I ran my tests. I started getting crazy errors that made no sense. After digging deeper my tests were failing because my model instances were missing first and lastnames. But because I made these changes on different days it didn't dawn on me right away what was happening coupled with the fact that the the app worked fine in development mode and tests would pass when run individually. Earlier today it finally clicked as to what the problem was and of course when I tested my theory, things were fixed. How's about cloning the array of accessible attributes since they're a class level collection that you're mutating there? Oops. Bad programmer. I know better but sometimes, well you just make mistakes.
cols = ContactInfo.accessible_attributes.clone
After making that small change everything was fine again and I was able to take out my gobs of debugging code in AR :-)
Post a Comment