Doing more Ruby on Rails today.
For ensuring data is good …
Anyone ever try using a scope to drop bad results and streamline view presentation?
Then just show the malformed entries on a different view where it doesn’t bork everything up?
Example I hit …
- if pet.user_detail
Revealed I had a single bad piece of data in my test results.
I’ve been playing around with rescue strategies etc for a year when something is missing.
Something like this guy does, though I used another’s idea last year … Link
Having struggled with this several times, I am considering the viability of a scope or series of them that presents only data that is valid & present. Something tells me there is probably a better way in code to do this, but the “If you absolutely & completely must be sure nothing should get to the view unless the record is good…” part of me is going “HRRRRMM”.
From time as a database admin, I would solve this issue in the DB, as a well formed one would have checks against bad data, but from a coders perspective there are more tools available and the database won’t always know what constitutes good data.
I was using a chunk of code that I found to be rather brittle for testing Xpath with Capybara in a ruby on rails integration test. Originally I was just calling
click_link on the first link within the
The problem is that’s really brittle. Any other links put in there before that will mess up the process. Also, if I make that page a partial so I can reuse that particular list on other pages, then my test is broken.
So my practice now is to add a class or div to the top of all partials or pages even if I have the body & nothing else is in that page.
_campaign.html.haml would have a
.campaigns at the top wrapping it. Then my html/css parsing with Xpath would be `.campaigns` to get into the section of the list I want to affect.
For my purposes that’s enough. If you want to go deeper though – as you have more elements inside & need to be selective, then you would add more path hierarchy …
.campaigns/.list-group/.list-group-item … with the last
 being select the first item in the list group.
Xpath best practices is to not stack too many or any nested items so they use
within. Still, probably best to only use two
within‘s one to grab the first unique class/div name & the next to grab the last div or element before running the method against the result.
- Instead the previous campaigns example, using the
within would be combined with something else….
Also, as a bonus I had to figure out the calls for using spaces in the names/titles. There are two methods I like.
- Exact matching with “multiple class selectors”…
.something/.something_else – Link.
- Matching Xpath to just search a string for a partial match with the xml/css – Link. I would still scope the search/match pattern (2nd method) by using a
.within anyways as the whole point is avoiding brittle tests.