Pickle (Cucumber) steps for verifying counter cache

This post was migrated over from Muziboo DevBlog. 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.

blog comments powered by Disqus
Hana Mohan
Hana Mohan