The Frontier Group - Blog

Using RSpec Example Groups for Common Functionality

January 13th, 2010, by aaron

I’m currently getting into using RSpec for testing our controllers on what is turning into a large project. It’s been more than handy because we have a lot of complex scoping to take into account whenever retrieving data. People don’t like to see other peoples’ financial data, mostly because it implies that someone is probably looking at theirs. With this in mind it’s more than important that we know the right data is going to the right places and hence the need for controller testing.

Now most of our controllers require the user to be logged in so writing tests to check this for every controller is annoying and time consuming, more than that it feels dirty. I think this is what some people call a code smell though I’m not up to speed on buzz words. There are also other tasks that are done quite often such as setting up the various types of users we’d like to test as, it would be nice if this were easily put in one place and could be easily pulled in. I guess I was looking for a template of tests that I could share.

It seems that the solution to it is found in Shared Example Groups which I hadn’t heard very much discussion about and it kind of leaves working out how they work to you rather than documenting it too much.

So far I’ve used it simply to make sure that controllers that require are redirecting users appropriately and also for setting up a specific type of user for our system before testing.

I created a directory under /spec/support called example_groups and in there I have a file called login_groups.rb. In that file I have something like the following :

shared_examples_for "customer is logged in" do
  before(:all) do
    @user = Factory(:customer_user)
    @user.customers.push Factory(:customer)
  end

  before(:each) do
    activate_authlogic
    OperatorSession.create(@operator)
  end
end

Now in my spec files when I have a bunch of tests requiring a logged in customer I will include this little snippet :

  it_should_behave_like "customer is logged in"

I get a logged in customer to start playing around with. I have the spec/support/example_groups directory in my include paths for Rspec and so it just all works.

My tests can then start to look like :

describe MerchantsController do
  it_should_behave_like "areas requiring login"

  context "customer logged in" do
    it_should_behave_like "customer is logged in"

    ... insert other tests here ...
  end
end

It means I can swap in another authentication gem/plugin pretty easily and also encapsulates the logic about creating customers, or whatever type of item you want to use, so that if that changes you can swap things in and out with a minimum of fuss.

Just to be clear, example groups aren’t limited to setup tasks or connecting to before/after hooks, you can also include a bunch of tests as well. This allows me to have a bunch of tests to run to make sure that a user does have to be logged in for various controllers and include these tests in on line.

I hope this helps someone, it took a bit of searching and trial and error myself this morning to get it working and find the uses for it that I’ve found. I’m definitely open to better solutions to this sort of issue though.

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.


Database Transactions in Cucumber Breaking Selenium

October 8th, 2009, by aaron

We’ve been using Selenium, Webrat, Cucumber and any number of other gems together for quite a while with very few problems. Just recently our Selenium tests just stopped working on the login step, and given our application requires users to login to do anything, it meant out test suite was basically broken from step one.

Given that we thought very little of any consequence had changed it was annoying trying to troubleshoot the problem. Typically you start off thinking it’s something you did, then maybe it’s something someone you know did, then you start to branch out and think that maybe, just maybe, it was a bug elsewhere!

I initially started to check the logs and they seemed perfect, but it wasn’t finding the user record in the database. So I loaded the database in sqlite and lo-and-behold there were no user records to be found. I started a debug session and it reported that the user had in fact been created. I thought it had to be something to do with transactions being used. Trying to insert a record into the database and being told the database was locked confirmed this.

I turned out that the Cucumber gem writer determined that there should be no more global setting for turning on or off transaction support when running tests. Previously transactions were turned off by default in Cucumber, and you’d generally turn them back on if you needed them. This change is annoying because a good chunk of our suite uses Selnium for testing and it requires transactions to be turned off. It’s also a bit odd to reverse a default option and have no way to enable it at all.

Apparently the work around is to tag all the features and scenarios that rely on non-transactional database operations with this new tag of @no-txn. After implementing this, we’ve found it does work and so our tests are back to usual condition.

So if you’re running into problems with your Selenium tests using Cucumber with Rails and your database isn’t updating within a test then check out the ‘@no-txn’ tag and see what happens after you use that.

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.


Understanding Ruby Blocks, Procs and Lambdas

January 7th, 2009, by aaron

I thought that I understood Ruby a little bit, but after reading Robert Sosinski’s article on Blocks, Procs and Lamdas in Ruby I felt a little humbled. To take such a small part of a language and express such detail and give some very useful examples really impressed me. The similarities between some of the ways I do things using Javascript and his examples really surprised me too.

I’d recommend giving it a good read to really understand how things work with these constructs and it may even give you a few new ideas on how to approach some problems you’ve encountered along the way.

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.


Rails Rumble Follow Up

December 8th, 2008, by aaron

I’ve written about Rails Rumble previously, the 48 hour event where teams turn an idea into a web application using Ruby on Rails. I was surprised to see the completeness of some of the applications that were developed this year as well as the utility of the ideas they implemented.

One of the more interesting articles I’ve seen in relation to Rails Rumble analysed the prevalence of various plug-ins and gems that teams utilised.

After a quick look there are a few points that specifically interest me :

  • jQuery is the most used Javascript library, even though it isn’t the default included with Rails. I think this says a lot about where jQuery fits in the client side coding space these days. Under further scrutiny it seems clear that jQuery had an even larger base of use than shown in that graph, as explained here.
  • 1 in 3 teams used a skeleton application. All of those teams used Bort. That’s a pretty overwhelming statistic for two reasons. Firstly it means you should be looking at using skeleton applications if you aren’t already. Secondly anything that you develop that could be used in other applications should possibly be a gem or plug-in. Reuse doesn’t seem to be something you just talk about anymore.
  • Over 50% of people wrote tests as part of their application, and the majority of people used a Behaviour Driven Development framework such as Shoulda or rSpec (they got rSpec!). Keep in mind that even on a tight schedule most people using Rails are writing tests!
  • Over a third of applications offered OpenID support for authentication. Given I don’t even remember where I signed up for OpenID this surprises me. Maybe it’s time to join this bandwagon?

I think the article gives a good indicator where you should look to implement certain parts of your application. Generally speaking, if a lot of passionate developers are using a particular library or piece of code then you’d be wise to make the same choice.

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.


What’s New in Rails 2.2?

November 8th, 2008, by mlambie

Last week Gregg Pollack from the Rails Envy Podcast put out the call on Twitter for reviews of their latest product, the Ruby on Rails 2.2 Envycast. I was happy to oblige and had been looking forward to checking out their unique take on screen casting for a while. Here’s what I thought.

Is it really a screen cast?

Firstly, I don’t even know if it’s correct to call it a screen cast. The viewer is not presented with a computer screen once during the 44 minute presentation. Most screen casts record the presenter’s computer desktop and have an audio commentary to accompany the video. Gregg and Jason have instead gone the “weather-man” approach and are using a green-screen as their canvas. This has some immediate benefits, but also has some downsides.

It develops the duo as the “face” of Envycasts. I first learnt of them through their audio podcast but they’ve done video before. I think this is important as it presents a teacher-student situation, and I’d expect there’s a bunch of psychology proving why face-to-face teaching succeeds. I think it works well for them, but would expect that other tech-screen casters might not be as comfortable in front of the camera.

The green-screen also allows them to manipulate the background as they see fit. For the viewer of this episode that means you’ll be treated to a world tour as they “travel” to various capital cities around the globe. It allows them to use their unique style of humour with ease too. I actually laughed out loud at a few moments, and enjoyed thembeing chased by the large ball of yarn in particular.

More importantly though, the green-screen also means that transition effects and attention-grabbing techniques (like exploding or rotating text) can direct your attention to the exact part of the code they’re discussing.

By not filming a mouse-cursor style screencast they’ve chosen to give up the opportunity to pass on subtle information outside of the scope of the specific topic that viewers might find useful. What do I mean? I’ve picked up on keyboard shortcuts or other techniques people use in their day-to-day programming by watching traditional screencasts. The best example I’ve found of this is Ryan Bates’ excellent Railscasts. I liken it to looking over the shoulder of a seasoned developer – you’ll pick up on nuances which are sometimes just as interesting.

Both Gregg and Jason speak clearly and are presenters that I enjoyed viewing. As an Australian, I didn’t find their accents overpowering in anyway.

It’s all about the content

The actual content is conveniently split into chapters (I viewed the 260MB MOV) which makes navigating between topics easy. I viewed the video on both a MacBook Pro laptop and 42″ plasma. I found the colours sometimes a little dark on the laptop but didn’t have the same issues on the plasma display. I suspect that my colourblindness was to blame :)

I would have appreciated a wide-screen format for the plasma, though I presume that the majority of viewers are watching on a computer screen.

The content is broken into the following seven sections:

  • ActiveRecord
  • ActiveSupport
  • ActionPack
  • ActionController
  • Railties
  • Internationalization
  • Performance

The 38 minutes (85%) are spent focussing on the code-heavy components which make up the changes to Rails 2.2. The examples given are very clear and consistent across the sections and I found the afore-mentioned transition effects helpful at tracking the exact part of the code the presenter was discussing. Your eyes are drawn to the moving parts of the code. Because you’re looking at Ruby code there’s nothing that will hurt your brain, or eyes.

The last few minutes in the presentation are spent discussing the other things that have changed with Rails 2.2 in details beyond just code examples. I enjoyed Jason explaining why gems are preferred over plugins, and Gregg’s explanation of etags and how they’ve changed in Rails 2.2 was fantastic.

Wrapping up

I was skeptical that this format would would for a such a technical topic but I’m glad to say they boys pulled it off. Their lame jokes are sometimes actually funny, though I think I’ve heard enough about Rails not scaling to last me a lifetime :)

My final thought: above all else they jammed a stack of detail into an easily-digestible format that was entertaining and informative. If you’re working with Rails then you really should be checking this out.

You can buy this Envycast along with the accompanying PDF online for just US$16, which even when you consider the current global economic situation is still a steal.

Disclaimer: The Frontier Group was provided with a free copy of this Envycast for review.

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.


Follow Us

Stay in the Loop

  • Enter your email address to subscribe to our mailing list. You'll get updates about our products, specials and bonus offers, and general behind the scenes news from our team.

Twitter

Newsletters

Alexa Rank

Testimonial

The boys at The Frontier Group are amazing! For such a relaxed and personable organisation, they have phenomenal technical ability and a rampant professionalism. They have customisable solutions for all of my IT needs and they always deliver, on time and beyond expectation.

They fix problems other service providers can't and they helped me get a critical section of my web site up and running 10 minutes after I emailed the request!

Alex Hyndman, Nexus Car Share.

Featured Project

Case Study - Caudo Group - www.caudo.com.au

Website

www.caudo.com.au

Caudo Machinery

Caudo Group engaged our services to redesign their outdated website. We sent our photographer on-site to capture the essence of their business and turned it into a stunning web design.