Jun
17
2010

Pickle (Cucumber) steps for verifying counter cache

Pickle is a great add on to cucumber as it provides steps for often used scenario. There is a step provided for verifying the association size of an active record instance. Therefore you can write something like

Then the user: “Joe” should have 1 friends

and it will check if joe.friends.size == 0. However this does not check the counter cache (if you have one). I modified it a bit to verify counter cache (changes in file features/step_definitions/pickle_steps.rb)

# assert size of association
Then /^#{capture_model} should have (\d+) (\w+)$/ do |name, size, association|
  if(model!(name).respond_to?("#{association}_count"))
    model!(name).send("#{association}_count").should == size.to_i
  else
    model!(name).send(association).size.should == size.to_i
  end
end

This assumes that you counter cache is named association_cache which is the convention in rails’ world.

posted in testing by prateek

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by openark.org