Unit testing has_one/belongs_to association in ruby model -RAILS

578 views Asked by At

I want to test the association in my models. I have two models

class Student < ActiveRecord::Base
  attr_accessible :card_id
  belongs_to :card
  has_one :card
end

class Card < ActiveRecord::Base
end

In my student_test.rb I have a test func.

def test_just_save
  stud = Student.create(:NAME => "Rabul", :SEX => "M", :STUDENT_ROLL_ID => 102, :CARD_ID => 3213123)
  assert !stud.new_record?
end

The test passes but CARD_ID 3212123 is not an existing id. I have not created any card and it is not in my fixtures.

Am I missing something in my models? I basically wanted to test the foreign key relationship between student and card.

Using ruby 1.9.3p484 (2013-11-22) [i386-mingw32] and rails 4.0.2

0

There are 0 answers