Easy Rails Testing

July 19th 2012

Every beginning Rails developer wants to tests their code. Unfortunately, most spend too much time deciding between Test::Unit and RSpec, get confused as to what types of tests to write, and eventually give up.

If you’re a beginner and want to test, start by learning how to write integration (Test::Unit) or request (RSpec) tests. These types of tests allow you to simulate what the user will actually do in the browser. Capybara is a gem that makes it easy to write integration/request tests. If you use Capybara, the difference between Test::Unit and RSpec is minimal.

Here’s a simple RSpec request spec (test) with Capybara for user sign up:

#spec/requests/users_spec.rb

require 'spec_helper'

describe "Users" do
  it "allows users to sign up" do
    visit root_path
    click_link 'Sign up'
    fill_in 'Email', with: 'user@example.com'
    fill_in 'Password', with: 'secret'
    click_button 'Sign up'
    page.should have_content('Welcome')
  end
end

You don’t need much programming experience to understand what’s going on here. The test is just going through the same sign up process that a user visiting your site would. The final line of the test page.should have_content('Welcome') is stating what the next page should show after a user clicks the ‘Sign up’ button. Simple, right? So go write some tests!

Check out Railscast #275 for more information on this type of testing.

A Solid Ruby Foundation - Code Academy Week 2

April 22nd 2012

I’ve been working hard for the first two weeks of Code Academy to build a solid foundation with Ruby. As I mentioned in my last post, when you work with Rails it can be easy to forget the magic that Ruby is doing behind the scenes.

To make sure that I don’t forget the basics of Ruby, I’ve outlined them over and over. Below is my most basic outline of Ruby concepts that I never want to forget. Some of the examples are taken from the excellent book Learn to Program by Chris Pine.

Arrays and Hashes

Arrays

Create new array and call 0 index:

a = ["scott", "mike", "joe"]
a[0]    # => "scott"

Add to array:

a << "mark"   #or
a[3] = "mark"

Hashes

Create new hash using symbol for key and call:

h = { :dog => "woof", "cat" => "meow" }
h[:dog]    # => "woof"

Add to hash:

h[:cow] = "moo"

Iterators

Each Method (p. 66)

languages.each do |lang|
  puts "I love " + lang
end

Integer Method (p. 67)

3.times do
  puts 'hip-hip-hooray!'
end

Methods

Methods with parameters (p. 76)

def say_moo(number_of_moos)
  puts 'mooooo...'*(number_of_moos)
end

say_moo(3)   # This is the method call with parameter

Initialize method is called automatically when new instance of class is created (p. 132)

Classes

Built in Classes (p. 117)

Typically, you just create new built in classes by setting values:

a = [1, 2, 3]   

But you are actually calling Array.new when you do this:

a = Array.new + [1, 2, 3]

Back to Basics - Code Academy Week 1

April 15th 2012

I made it through my first week of Code Academy. Week 1 was all about building a solid foundation with the basics of Ruby. For me, Ruby has been much more difficult to learn than Rails. I think the reason is that learning Ruby can be a bit boring at first. The best way to learn a programming language is to build something with it. Unfortunately, there are not that many interesting things you can build with basic Ruby alone (at least not interesting to me).

With Rails, on the other hand, you can get up and running and build your own blog or small app pretty quickly. If you really want to dive into Rails you can do the awesome Rails Tutorial and build a functioning twitter-like app.

When you’re working with Rails, it’s easy to forget the magic Ruby is doing behind the scenes. You can create a Rails app with very little knowledge of Ruby, but that doesn’t mean that you should. I think that learning Ruby from the ground up will allow me to have a much better understanding of what Rails is actually doing. So for the next week, in addition to class, I’ll be diving deep into Chris Pine’s Learn to Program.

The Code Academy Experience

Beyond getting started with Ruby, the first week of Code Academy was a blast! I really enjoyed meeting and getting to know my classmates in the spring development class. Everyone is focused and interested in learning as much as possible. It was awesome to spend time with so many people who are as interested in web development as I am. I’m really looking forward to the next 10 weeks.

More Posts