I've started specifying a project and could use some help. I'm not sure if the spec is entirely necessary, it would be great to get some guidance on a best practice or something close.
I recently realized the need to add the method below grouped_by_months and started to write a spec.
idea 1: {{{ before do @comment = Comment.new @comment.stub!(:created_at).and_return(Time.now -2.days) end
it 'should return comments grouped by month' do group_terms = lambda{|comment| comment.created_at.beginning_of_month }
Comment.should_receive(:find).and_return([@comment]) [@comment].should_receive(:group_by).with(&group_terms).and_return([[@comment]])
Comment.grouped_by_month end }}}
idea 2 {{{
before do @comment = Comment.new @comment.stub!(:created_at).and_return(Time.now -2.days) end
it 'should return comments grouped by month' do group_terms = lambda{|comment| comment.created_at.beginning_of_month } lambda{ Comment.should_receive(:find).and_return([@comment]) }.should_receive(:group_by).with(&group_terms)
Comment.grouped_by_month end }}}
Class with method {{{ class Comment < ActiveRecord::Base
validates_presence_of :email, :body validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => 'invalid email address'
def self.grouped_by_month find(:all).group_by{|comment| comment.created_at.beginning_of_month } end
end }}}