4

I m working on rails project and i m new on rails and I want to test my model relationships and methods in model using rspec. How could I do this my model relationships are like this

class Idea < ActiveRecord::Base

  belongs_to :person
  belongs_to :company
  has_many :votes
  validates_presence_of :title, :description
  def published?
    if self.status == "published"
      return true
    else
      return false
    end
  end

  def image_present
    if self.image_url
      return self.image_url
    else
      return "/images/image_not_found.jpg"
    end
  end



  def voted
    self.votes.present?
  end
end

My idea_spec.rb file is

require 'spec_helper'

describe Idea do

  it "should have title" do
  Idea.new(:title=>'hello',:description=>'some_desc').should be_valid
  end

  it "should have description" do
  Idea.new(:title=>'hello',:description=>'some_desc').should be_valid
  end
end
4
  • You only to write specs for code you write. Adding tests for features provided by framework is not a good idea. Commented Feb 7, 2012 at 11:59
  • 1
    I agree in part with @Swanand's comment, but this is a bit more subtle. There is a fundamental difference between associations (which are provided by the framework) and validations (which are also provided by the framework). Associations are structure, which need not be tested directly because it should only exist to support behavior, which should be tested. Validations, however, are behavior, and should be tested. My 2 cents. Commented Feb 7, 2012 at 13:46
  • @DavidChelimsky - Sure, I agree. My only concern was, validations are more like DSL here, provided by rails and were already tested when the rails build passed. But I'll take your word for it (I already did actually, by reading The RSpec Book). Commented Feb 7, 2012 at 14:00
  • Blog post inspired by this question: blog.davidchelimsky.net/2012/02/12/… Commented Feb 12, 2012 at 23:01

2 Answers 2

5

If you use shoulda-matchers (https://github.com/thoughtbot/shoulda-matchers) gem you can write these as it{should belong_to(:person)}

But to be honest I don't get a lot out of these types of test, AR is well tested.

Sign up to request clarification or add additional context in comments.

10 Comments

I use shoulda-matchers and always add such specs, since it's so easy and quite reliable. They test several things including presence of the foreign key in the database etc.
True, but for uniqueness tests they force you to hit the db when unit testing. I'm not saying it is wrong to use these tests, I'm saying I personally don't get much out them as I have confidence in AR. If you are not confident of the code then you should test it, that's good practice.
It is indeed wrong to test this. That just shows our distrust for an open source product. The tests are already added and passed. Why do we want to test it again?
I would say the only purpose to test the relations and validations - is to be sure we did not forget to add them :) But testing with shoulda-matchers is more than enough
@Swanand: I don't distrust AR and don't test ARs behavior. I test the correct usage of a feature provided by AR. There are things you can break, for example, removal of the foreign key in the db, if you rename a class or remove the middle model in a has_many :through relationship.
|
-1

Simple answer is, you do not. Neither do you test has_many or belongs_to. The place for these specs / tests is in Rails codebase, and not in your codebase. You Idea spec gives you absolutely nothing that Rails does not.

Here are the official tests:

  1. belongs_to
  2. has_many

Edit: Please read @DavidChelimsky's comment above.

3 Comments

looks like you haven't got question right... he is not asking testing of base association methods. He wants presence test of associations inside his own model and that can be done by using shoulda-matchers
He says "I want to test my model relationships and methods in model". Also, what is the difference between "base association methods" and "associations inside his own model"? Aren't they the same?
i mean to say base methods has_many, belongs_to and associations means belongs_to :person which is he wanted to test.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.