Topic: Should I even have this test??

I'm new to testing and am trying to write some good tests but was wondering if this was even useful:

=user_spec=
    it "should increment post count by only 1" do
        @user.attributes = valid_user_attributes
        old_count = @user.num_posts
        @user.add_post
        @user.num_posts.should == old_count+1
    end

=user.rb=
    def add_post
        if !self.num_posts.nil?       
            self.num_posts+=1
        else
            self.num_posts = 1
        end
    end

---Seems kind of stupid to me, no? Are these the kind of tests I should be writing? Thanks

Re: Should I even have this test??

I would test this. For one thing it's easy to test (unlike views for example) and there's definite logic here. You might also want to try refactoring it and improve it which makes tests invaluable because you can instantly see if you broke anything. Here's how I would test it:

it "should increment post count when calling add_post" do
  lambda { @user.add_post }.should change(@user, :num_posts).by(1)
end

it "should set post count to 1 when calling add_post when it is nil" do
  @user.num_posts = nil
  @user.add_post
  @user.num_posts.should == 1
end


Notice this is two specs because there's two different paths here. If you only test one path you don't have good coverage. You can then try refactoring it to this for example:

def add_post
  self.num_posts ||= 0
  self.num_posts += 1
end

Railscasts - Free Ruby on Rails Screencasts

Re: Should I even have this test??

awesome! thanks a lot for the tips! I'll have to familiarize myself better with the lambda method  since i've seen it in so many places. Google/Rdoc, here I come