Archive for June, 2009

h1

Jumping on the Rails Pt.2 – RSpec, Cucumber & Webrat

June 27, 2009

Well it’s been a while since I’ve written anything really technical & seeing as Rails is taking up a lot of my personal time, I thought I made sense to put some of my newly found knowledge together.

I thought it best to not go over material that has already been done and very well I might add, I’ll just focus on the things have have interested me most & how to take advantage of them in real world situations. This and the next few posts will focus on developing and application using Cucumber, Rails & Webrat to drive the development process. Before I go on, I’d say that none of this would be possible if not for The RSpec book, I’ve read it for a couple times whilst the beta has been floating about and recommend the book to everyone developing in Rails, if only to see what it has to offer.

Lets start of with a basic application. StoryBoard, allows users to create & manage projects and their features with a focus on capturing features that can be used with Cucumber.


rails PROJECTNAME
cd PROJECTNAME

Now we need to setup our BDD environment


script/plugin install --force git://github.com/aslakhellesoy/cucumber.git &&
script/plugin install --force git://github.com/dchelimsky/rspec.git &&
script/plugin install --force git://github.com/dchelimsky/rspec-rails.git &&
script/plugin install --force git://github.com/brynary/webrat.git &&
script/generate rspec &&
script/generate cucumber

Now that’s all we really need to setup our BDD environment, let start with a basic feature to get our feet wet:


Feature: Users must be able to register to the site

In order to allow new users to join the site they need to be able to view the home page & register.
As a guest user
I want to be able to register to the site so that I can create new scenarios for a project

Scenario: A user must be able to view the welcome page
Given I am not logged in
When I visit the home page
Then I should see the sign up link

Seems reasonable, users should be able to view the home page and see a sign up link, after all no one can register without one.

Now a nice little gift that comes with our plugins is autospec, I added the following to my .bash_profile

export AUTOFEATURE=true

This allows me to test both my specs & step definitions.

Starting script/autospec allows us to run our tests automatically on ever save, which can be a nice time saver for those who forget to test after a change.

You can implement step definitions for undefined steps with these snippets:
1 scenario (1 undefined)

3 steps (2 undefined, 1 passed)
0m0.050s

You can implement step definitions for undefined steps with these snippets:

Given /^I am not logged in$/ do
pending
end

When /^I visit the home page$/ do
pending
end

Then /^I should see the sign up link$/ do
pending
end

Cucumber is nice enough to give us code snippets to use in our step definitions, lets start with the first.

Create the file registration_steps.rb

We’ll cheat here seeing as we don’t need to set anything up, it passes.

Given /^I am not logged in$/ do
end

That gets our first part, lets move on to the next, again cucumber will nicely give us the next step definition, we’ll modifiy to look like the code below.

To take advantage of webrat within our specs we’ll need to add the following to our spec_helper.rb

require 'webrat'
config.include Webrat::Matchers, :type => :views

It should now look something like this:

ENV["RAILS_ENV"] ||= 'test'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
require 'spec/autorun'
require 'spec/rails'
require 'webrat'

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

Spec::Runner.configure do |config|
config.use_transactional_fixtures = true
config.use_instantiated_fixtures  = false
config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
config.include Webrat::Matchers, :type => :views
end

Now for the next step definition

When /^I visit the home page$/ do
visit '/'
end

Ok, we obviously need to setup a route for this one.

map.root :controller => "welcome"

We get another error saying:

uninitialized constant WelcomeController (NameError)

Okay we know we don’t have a controller, how about we cheat and make one just to make things pass for now. Especially as we don’t want to get bogged down with detail.

script/generate rspec_controller Welcome index

That will basically create our welcome page for us. We can now move on to the next step definition.

Then /^I should see the sign up link$/ do
response.should contain 'Sign up'
end

Now our tests are back to failing again, we’ll move to the controllers view to iron out our functionality.

Own spec_controller was nice enough to create our view for us, lets open up the view spec and add a spec.


#Delete this example and add some real ones or delete this file
it "should tell you where to find the file" do
response.should have_tag('p', %r[Find me in app/views/welcome/index])
end

Is our first test in our view spec, lets replace this with something more meaningful.


it "should have a sign up link" do
response.should have_selector("a", :content => "Sign up")
end

Here we just want to make sure that we have a page where we can sign the user up. Let get it to pass, remove the index.html from the public directory, the add the code below to your Welcome controllers index view.

<a href="/signup">Sign up</a>

Nice and simple, right. Now we’re still got alot of work to do but that is what the other stories are for ;) , we’ll follow up with those in the next post.

h1

Jumping on the Rails

June 21, 2009

Well on my journey to improve my programming skills & again purely because I’ve been getting tired of using PHP for all my solutions, I recently decided to delve into Rails.

I’d been pointed to it by Ekerete Akpan an old work collegue who uses it quite extensively with his own projects.

I’d resisted from learning the language initially primarily down to the fact that the language seemed to be quite basic, in that I was correct, something I’m regretting as I find it now to be one of the best features.

I initally wanted to create a frontend for Chatterl, using ActiveResource. After fighting with it for a week or so, I ended up scrapping that notion and moving on to another project idea I had kept aside for a little while (Solvius).

I won’t go into detail on the goals of Solvius as the site is already live & a brief summary of the webapp can be found here.

The first thing I did was get my self a copy of The Rails way, I have to say I’ve not used the book much over the past month but it was indepensible when it came to helping me learn the basics. I had also picked up copies of ‘Agile Web Development with Rails‘ & ‘Rails for PHP Developers‘, though these proved to be little help & ended up being frustrating reads as they both presumed the reader had basic knowledge of programming methodologies.

By this time I had built the basic functionality of Solvius, allowing users to join the site and create and solve problems, using the active as authenticated plugin. My journey was far from over, I wanted to add ReCaptcha & a little JQuery to the mix. I got a little excited thinking that I could get my hands dirty but low and behold there were plugins for both of these features also :D . Such things are a rarity in the PHP world and having spent a great deal of time there, the PHP side of me felt robbed, whilst the code monkey in me was pleased that I could deal with more direct problems.

All though my inital experience was a pleasant one, I wanted to improve on it further. I’ve been using TDD for a while and played around with Test::Unit for a bit but wasn’t really happy using it, I decided to go for my next book purchase ‘The RSpec Book‘. I have to say reading that was an eye opener, not only from a BDD newbie stand point but also from a coders point of  view, the book helped to improve my coding style and gave me a few cool ideas to play around with when I get the chance.

I started introducing RSpec & cucumber into Solvius about a week ago & already started to reap the benefits, primarily being able to implement functionality from a users point of view rather than my techie one, along with having a point of reference and an enhanced sense of confidence in implementing new functionality.

I’ve read ‘The RSpec Book‘ a couple of times now and will hopefully get the chance to use Cucumber & RSpec in my next pet project. For the moment though I’ll focus my time on improving Solvius & improving my Rails skills in general. Although it feels as if I have picked up the framework pretty quickly, there is still a host of things to grasp & learn.

I’m hoping I’ll have enough time to create a serious of posts, outlining the whole development process using Rails & the tools I’ve picked up over the course of my Rails journey.

h1

Latest project – Solvius

June 7, 2009

Well it’s been a while since I’ve written a post, mainly due to the amount of time I’ve spent learning Rails & working on Solvius.

After reading “Pragmatic Thinking and Learning: Refactor Your Wetware“, I decided to create a program to store all the problems I come across over times, storing them in a solutions log of sorts. For one reason or another I had put it off.

Fortunately I came up with some free time & decided to finally write the application in Rails, I’m new to Rails & as some of you may know I rarely miss the chance to kill two birds with one stone.

It didn’t take long to create the basic functionality, two weeks at the most, the last few weeks I’ve spent learning Cucumber & RSpec, but thats another post. I’ve finally uploaded the application to my host & setup some basic data, over the next few weeks I’ll incrementally add new functionality to the site.

It’s been an enjoyable experience, I must say I’ve become a total convert to rails (i’ll leave that for another post) & have picked up a crap load of knowledge over the past month of a half learning. Hopefully in the coming weeks I’ll write up my notes and experiences but for now I thought I’d quickly go through what Solvius allows people to do & some of the plans I have for it in the future.

Why

Well as I previously mentioned Solvius was inspired by “Pragmatic Thinking and Learning: Refactor Your Wetware“. It’s primary goal is to help people track and store problems & solutions that we come across in our day to day lives. I’ve lost count of the amount of times I’ve come across a problem that I’ve solved but know I’ll have to go digging for the solution.

The cool thing about solvius is that the application can be used to store all kinds of problems and solutions, whether it be some kind of novelty puzzle to some a professional or academic conundrum.

At the moment the application is in beta, so invites are needed before you can signup, hopefully once all the kinks are ironed out and I have the time to improve the design, the site will be open to all.

Features

  • Create problems
    Allows a user to create a public or private problem
  • Solve problems
    Registered users are able to solve public problems, as well as their private ones
  • Make problems private
    Not everyone likes their problems out in the open

Future Features

  • Invites via twitter
    Will be able to tweet @solvius which will mail out an invite
  • Suggest solutions
    Users are able to suggest solutions for public problems
  • Solution & problem search
    Registered users will be able to search for specific types of problems & solutions
  • Facebook Connect
    Peoples will be able to register via FBConnect
  • Tweet updates
    Updates when problems are solved
  • Problem/Solution groups
    Users will be able to join specific problem/solution groups to help in mass problem solving.

Feel free to check out the site out  or tweet @solvius with any suggestions or ideas.