I have a user model and a friendship model.
class Friendship < ActiveRecord::Base belongs_to :sender, :class_name=>'User', :foreign_key=>'sender_id' belongs_to :receiver, :class_name=>'User', :foreign_key=>'receiver_id' validates_presence_of :receiver_id, :sender_id validates_associated :receiver, :sender end class User < ActiveRecord::Base has_many :sent_friendships, :class_name => "Friendship", :foreign_key => "sender_id", :dependent => :destroy has_many :received_friendships, :class_name => "Friendship", :foreign_key => "receiver_id", :dependent => :destroy end
and one of my rspec tests is
describe Friendship do before(:each) do @valid_attributes = { :sender_id => 1, :receiver_id => 2, :accepted_at => nil } end it "should not be valid with non-existant receiver_id" do f = Friendship.new(@valid_attributes) f.receiver_id = 99999 f.should_not be_valid end end
The test does not have to be valid because there is no user with user_id 9999. But the test says that the friendship model is valid.
Why the hell?
EDIT:
But I want to test, as mentioned, β without directly assigning the sender. Is it impossible?
ruby ruby-on-rails
Lichtamberg
source share