getting started with Cucumber and Webrat on Rails

A couple of months ago I started using Cucumber in one of my projects. I think it’s a great tool for large projects. It makes it easier for developers joining the project and it gives the development team a lot of confidence when changing the code.

If you have a running set of tests, you know that you can go in, write a failing test, modify the code, add a feature and then run the tests again. If the tests fail, you broke something that was working: fix it. If you didn’t, great news: you have a new working feature!

There is a great Railscasts that explains how to get started: http://railscasts.com/episodes/155-beginning-with-cucumber

Here is a set of steps to follow.

1. Create your Rails app

rails blog

2. Add Cucumber and Webrat to your environment.rb

config.gem 'cucumber',    :lib => false,
  :version => '=0.4.3'
config.gem 'webrat',      :lib => false,
  :version => '>=0.5.3'
config.gem 'cucumber-rails',      :lib => false,
  :version => '>= 0.3.1'

3. Install your gems

cd blog
sudo rake gems:install

4. Generate your Cucumber code

cd blog
./script/generate cucumber --webrat

3. Start writing feature files

For example:

vi features/list_posts.feature

Feature: Listing all the posts in the blog
  As a user
  I want to see all the posts in the blog
  Scenario: I go to the posts list page and I see all the posts
    Given there is a post titled "Awesome post" with content "My cool content"
    When I go to the posts page
    Then I should see what is up
    And I should see "Awesome post"
    And I should see "My cool content"
    And I should not see "New post"
4. Start fixing failing tests
Cucumber will show you:
  • Passing steps
  • Skipped steps: These steps weren’t evaluated due to failing or undefined step
  • Failing steps: These steps don’t pass. Something isn’t working as it should
  • Undefined steps: These steps aren’t defined in features/step_definitions/*.rb – You should define it or make sure you spelled this step correctly.
Webrat provides you a lot of useful steps to get started. You can see them in features/step_definitions. I encourage you to go in and check it out.
5. Create features/step_definitions/general_steps.rb and put this in:
Then /^I should see what is up$/ do
  save_and_open_page
end
6. Create features/step_definitions/blog_steps.rb and put this in:
Given /^there is a post titled "([^\"]*)" with content "([^\"]*)"$/ do |title, content|
  Post.new(:title => title, :content => content).save
end
Note: After modifying the database, you will need to run rake db:migrate on your dev environment and then rake db:test:prepare to clone the changes to the cucumber database.
Have fun cuking!
Posted in open source, programming, web programming | Tagged , , | Leave a comment

please stop overdesigning!

Keep it simple, stupid. Or keep it simple stupid. Or keep it simple and straightforward.

But please, do it for other programmers. Do it for yourself, for your own sanity. Stop overdesigning and ‘designing for the future.’ Stop designing for ‘what may happen in the future.’

You don’t need utterly complicated designs. You are not an oracle and you cannot tell what your application will need to do in the future. So please solve a concrete problem and move on.

95% of the time you are overdesigning your code. Solve a problem now. Refactor later. Thank you!

Posted in IT services business, programming, software engineering | Tagged , , , | Leave a comment

rails-authorization-plugin first impressions

I started using the rails-authorization-plugin last week. It is a really awesome and flexible plugin. I don’t have much to say about it yet.

But there is one thing that I find confusing:

In order to set a role for a user, you can do it like this:

user.is_admin_for(group)

To check if a user has a role, you can do it like this:

user.is_admin_for?(group)

See the subtle difference? Well, I think the difference is too subtle. Is there a better way to set a role for a user? Haven’t found it yet.

Other than that, the plugin is awesome. It’s super flexible and the integration is pretty seamless. Can’t wait to learn more about it.

Posted in developer tools, web programming | Tagged , , , , | Leave a comment