Posterous theme by Cory Watilo

Testing Rendered Views with RSpec2, Capybara and Rails 3.

While view tests are brittle, easily breaking when the design of a page changes, they are undoubtedly handy for checking the important parts of a page are rendered. I usually check for page titles, model attributes/tables or forms, and footer element, and so on.

Capybara is a great DSL for testing a page’s rendered output, but unfortunately the Capybara matchers are not available in view or helper specs according to the rspec-rails documentation.

Thanks to this answer on Stack Overflow, it’s possible to load a page’s rendered content into a controller spec, and test it using the Capybara matchers.

I’m posting some example code here based on the answer for future reference, and to help anyone struggling to use Capybara with Rspec 2:

# Gemfile
group :test do
  gem 'capybara'
  gem 'rspec-rails'
end


# spec/controllers/users_controller_spec.rb
require 'spec_helper'

describe UsersController do
  # Render views and load the rendered content into Capybara for matching
  # See: http://stackoverflow.com/questions/4706370/rspec-view-testing-with-capybara-and-rails3/4773050#4773050#
  render_views
  let(:page) { Capybara::Node::Simple.new(@response.body) }

  describe "GET 'index'" do
    before { get :index } 

    it { should respond_with(:success) }
    it { should assign_to(:users).with_kind_of(Array) }
    it { should render_template(:index) }

    # Test the rendered view using Capybara matchers
    it { page.should have_selector("h2") }
    it { page.should have_content("All Users") }
    it { page.should have_selector("table#users") }
    # ... add more tests ...
    it { page.should have_selector("div#footer" }
  end

Helpful post? Let me know by commenting on Twitter or Google+.

Moving to Posterous

After months of deliberation, and concern about breaking permalinks, I’ve finally decided to make the move from a self-hosted Wordpress blog to posterous.com. There are arguments for and against, but my decision came down to the following.

Delegation

Using posterous means I no longer have to worry about updating software, managing databases and plugins, or backing up my content.

Email Interface

The feature that first attracted me to posterous is being able to easily post through email. Email is one of the fastest and most universally available interfaces to any software, and the more I can work in gmail without having to resort to some horrible javascript text editor, the better.

Less Resistance

Posterous' simplicity forces you to worry more about posting content and less about the environment. Unlike Wordpress, the whole posterous experience feels light and quick – there is little resistance to post.

Simplifying Conversation

For years I’d seen comments as a great way to facilitate conversation from my posts, and Posterous' lack of ability to import Disqus was what held me back. Recently I realised that today there are so many conversational tools already available, why add another one to the mix? I’ve removed comments from the blog – you can still reply to posts by posting a message to me on Twitter and Google+

The overwhelming reason of my move is simplicity – worrying less about infrastructure and management, and more about posting content. Hopefully Posterous will help me to post more articles and tutorials. For photos and “microblog” posts, I’ll continue to use Twitter and Google+.

Permalinks

As posterous enforces a different permalink structure to Wordpress, I’ve put together a pretty hefty htaccess file to ensure old links still work. For reference, my old Wordpress permalink structure was:

/yyyy/mm/dd/post-title-of-any-length

Posterous uses the format:

/post-title-up-to-45-characters

Some of my permalinks omitted the day (/dd/) entirely. Here are the Apache rewrite rules I’ve used to redirect posts:

# /yyyy/mm/dd/post-title

RewriteCond %{HTTP_HOST} ^chrisblunt\.com

RewriteCond %{REQUEST_URI}% ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.{1,45})(.*)$

RewriteRule ^[0-9]{4}/[0-9]{2}/[0-9]{2}/(.{1,45})(.*)$ http://blog.chrisblunt.com/$1 [R=301,NC,L]



# /yyyy/mm/post-title

RewriteCond %{HTTP_HOST} ^chrisblunt\.com

RewriteCond %{REQUEST_URI}% ^/[0-9]{4}/[0-9]{2}/(.{1,45})(.*)$

RewriteRule ^[0-9]{4}/[0-9]{2}/(.{1,45})(.*)$ http://blog.chrisblunt.com/$1 [R=301,NC,L]

The rules aren’t perfect – posterous changes some permalinks, specifically those that include numbers and underscores (_). For now, I’ve just writen redirects for each affected post.

If you find a broken link, please let me know through email (chris at chrisblunt dot com), Twitter or Google+.

Android: How to test Local Web Apps on an Emulated Device (AVD)

Editing your /etc/hosts file is great for running and testing web apps as they are under development. For a project I'm working on, I needed to be able to access one of my local Rails apps through an Android device, which meant editing the device's /etc/hosts file. As I haven't rooted my Nexus-S, so opted to use an emulated Android Virtual Device (AVD), as they are configured by default to allow root access. I started by creating a simple hosts file on my desktop. It's this file that I would push to the AVD
cd Desktop
vim hosts # or your favourite text editor....
Use your text editor to create a simple hosts file pointing to your local web app. Remember to also include a localhost entry:
# hosts
  127.0.0.1       localhost 
  192.168.1.10    rails-app
Next, copy your new hosts file to the AVD:
adb push hosts /etc/hosts

Access Denied

Despite having root access, though, adb wouldn't let me push the new hosts file because the system partition is mounted as read-only. You can remount the system partition in read-write mode using adb remount, and then try pushing the hosts file up to the device:
adb remount
abd push hosts /etc/hosts
I kept getting an Out of Memory error, which seems to be a common problem. The solution (gleaned from scattered forum posts) is to use the emulator's -partition-size option. Using this option means you won't be able to launch the AVD directly from Eclipse, but instead need to use the command line. Close down any running AVDs, and then run, and then re-launch the AVD passing in the -partition-size option with a reasonable value:
emulator -avd Samsung_Galaxy-Tab -partition-size 128

# ...wait the avd to boot up...

adb remount
abd push hosts /etc/hosts
The hosts file should now successfully be copied to your AVD. If you launch the AVD's browser and enter the local URL from your hosts file (e.g. http://rails-app:3000/), the AVD will connect to the local IP address you specified. Note: You'll need to keep hold of your hosts file as your settings will be wiped when you shut down the AVD. You can reinstall the file next time by performing the adb remount; adb push hosts /etc/hosts script each time you boot the AVD.

References

  1. Google Groups Thread
  2. Cute Android Tips: Failed to Copy File to System

Thoughts: On Agile Project Estimating and Pricing

Media_httpchrisbluntc_lqefb
The usual approach to pricing service work is the hourly rate - a simple model that is easily understood, and translates well across different disciplines and businesses. Service work often has so many variables that an hourly rate seems to offer a good compromise between a fixed-price quote, and ensuring the supplier has flexibility to charge for unknown future work. However, in practice, the hourly rate is a poor option for both parties. Taking software development as an example, it can be hard as a supplier to demand (or not to demand) extra fees for hours incurred over original quote. For the customer, there is the worry that the hours will rack up quickly without results, or that she may end up paying for unused hours. At its most basic level, rewarding time with money encourages laziness - what incentive is there for a supplier to finish the job quickly, only to be paid less? Charging an hourly rate really does no favours for either the supplier or the customer, yet continues to be the status-quo, as it's hard to find any simple alternative.

The Goal: Remove Time from the Equation

Like others, one of the many reasons I set up business was to escape the confines of time equals money. In fact, as any business owner will likely attest, you'll often spend considerably more time working on your business than for which you'll ever see financially reward - but that's all part of the fun! In trying to break the reliance of time from income, I started looking at different ways of estimating and managing my client projects. This is usually achieved in two ways: employ people, and generate profits on their time; or build and sell assets. For a small software company, assets are often the dream: a product - or range of products - that keeps selling, and requires roughly the same investment in time whether you've sold one or one thousand. Whilst this is the dream, there is more often than not a huge upfront cost involved. If you're bootstrapping your company, it's likely client work will be essential in generating income to cover your bills.

Trial and Errors

After a few projects based on a standard hourly rate, I decided to embrace iterative development. The idea was to charge on a per-iteration development cycle. Each iteration was a fixed period of time (usually 2 weeks), and was charged at a fixed price. The amount of work completed in this cycle would be used to estimate the amount of work that could be completed in the next iteration. This was very simple interpretation of agile project management with velocity tracking. The process seemed to make sense, but in practice was still bound to time. Essentially, I was quoting on a bi-weekly rate rather than an hourly rate. This resulted in the same problems, just without the fidelity of tracking individual hours. Another realisation was just how rarely projects fit into the neat time-boxes we assign them. Often an iteration would extend beyond 2, or even 3 weeks, simply because the day-to-day running of business (for both parties) takes time. In this situation, it would be completely inappropriate to charge for 2 iterations, but with the project held up, and my velocity was reduced to 1! I'd fallen into the trap of worrying more about the process (paperwork!) than the end-result. It was clear I needed to completely remove time from the equation, and concern myself with output.

Focus on Results: Agile Project Estimation & Tracking

With this in mind, I've decided to revise my iterative estimation and projection process using a simplified take on the agile process. Throughout a project, each user feature (or story) is assigned a number of points based on its estimated difficulty. The majority of stories should be 1 point, a story that is known to prove difficult is assigned 2 points, and an unknown (e.g. developing something that is likely to require a high degree of research) is assigned 3 points. There are alternatives, such as using the Fibonacci sequence, but to my mind the simplicity of this model is an advantage for both me and the customer. Rather than charging by time, I'll cost a project on the total number of points it contains, with each point priced at a known constant. For example, assuming a £200-per-point price, then a 2-point story would be charged at £400. Of course, the accuracy of using this approach is in gathering, and successfully distilling, initial requirements. But by breaking the project into achievable stories and points, the customer can see a break-down of exactly where their budget is being spent, and revise or prioritise accordingly. From the developer's point of view, this approach maintains the flexibility that comes from hourly rates. If the customer requests new or changed functionality, then new features (and hence points) will be allocated to the project. I'll continue to use the iterative project approach in estimating how much work can be completed within a fixed time-frame, and continue to track velocity so that the accuracy of these estimates can be improved.

Things Will Change: Stay Agile

There are, of course, going to be imperfections in the process. This will certainly be the case with the subjective parts of a project. Stories are necessarily concrete and describe functionality, so how can points be assigned to the graphic design of an application's interface? I think this will just have to be left to experience of estimating, but I hope that using a pricing structure that is measurable, flexible and rewards efficient and effective development will prove beneficial to both parties. I'll post updates on how this approach goes, but in the meantime please feel free to discuss your thoughts in the comments below.

Resource: Google Nexus-S Graphic for Inkscape (SVG / XCF)

Media_httpchrisbluntc_yaoqb

I needed a reasonable representation of the Google Nexus S for my site and some client proposal work. There are some stunning examples out there already, but those I found were generally in PSD or non-scalable PNG formats.

Being a long-time Ubuntu convert, I needed something that was scalable and worked nicely with Inkscape and GIMP - so I set about drawing my own. The graphic is far from pixel-perfect, but gives a good representation of an Android device for proposals and mock-ups, etc.

The XCF is sized so the screen area is 480x800; the SVG is of course scalable. This is my first release. The source files in SVG and XCF format are included in the download, and licensed under MIT. Free to use and improve however you wish.

Download .zip