SHARE:
Uncategorized

Testing Tools in Rails Application Development

Flatiron School / 13 December 2012

The following is a guest post by Jenya Zueva and originally appeared on her blog. Jenya is currently a student a The Flatiron School. You can learn more about her here, or follow her on twitter here.

Did you ever wake up and thought “Today, I’m going to write the entire app in one attempt”? How did it go for you?

One of the hardest thing for me while learning coding was to understand what my lines of code actually do. I would write ten lines of code only to see it do nothing, and have no idea where to start debugging it. I would assume my entire logic was incorrect, erase it all only to find out later that I used .each instead of .map.

My biggest AHA moment was to discover tools available to test what my code does as I develop my functionalities. Let’s take a look at few examples I cannot imagine my day without.

Use Irb to Master Ruby

Run irb in your console to access Interactive Ruby console. Can’t figure out if you need to use .map or .each Test it.

Ohhh, now it makes sense. My .each returns the original value, and .map returns the outcome. Easy!

“Puts” in Ruby

Nokogiri scraper goes through the page and collects values that you can store in the database for later use in the application. Simplified version of my personal scraper project looked like this:

Run this file in your Console to see the result:

It might or might NOT show lines that look like this:

We got an error. (Surprised? Don’t be). Let’s start debugging. I’m going to comment out the call to the database and ask ruby to print the value of title on the screen.

This will return:

What does this tell us? Too much information!!! Let’s refactor our code to only return the text from the title tag.

Fantastic. This works. Database is populated.

Rails C in Rails Environment

Consider these two methods:

One will show you all of the repos available, the other will only show repos that do not already belong to the user. How can I make sure that this method works correctly? Fire rails c in your active application folder and test it.

‘Inspect Element’ in Chrome

Chrome offers several nifty features that make developer’s life easier. Right-click on the element => ‘Inspect Element’ or Command+Shift+c will bring up Developer Tools window on the bottom of the screen. Console Tab can be used to inspect the DOM, debug JavaScript or analyze HTML parse errors.

Remember the example I displayed at the beginning of this post in my Nokogiri project?

Looks familiar?

Be smart and seek out helper tools to do your work efficiently.
Test lines your don’t understand.
Test methods you just created.
PLEASE, Work off a separate branch.
Test your commits.
Enjoy it 🙂

Wrapping a Legacy Database With Active Record Previous Post Google Maps for Rails Next Post