Hi everyone,
I'm having a devil of a time trying to figure out why this spec keeps giving me an error. I'm attempting to mock out a class I haven't created yet (User) so I don't need to worry about creating it at the moment. I could have sworn I read somewhere that it was possible to pass a mock object as an attribute to an associated class, but it's just not working for me.
Here's the code I have:
# task_spec.rb describe Task do before(:each) do user_proxy = mock('user') @task = Task.new( :description => "Sample task description", :assigned_user => user_proxy ) end it "should be valid with all required attributes" do @task.should be_valid end it "should be invalid without a description" do @task.description = nil @task.should_not be_valid @task.should have(1).error_on(:description) end it "should be invalid unless assigned to a user" do @task.assigned_user = nil @task.should_not be_valid @task.should have(1).error_on(:assigned_user_id) end end
# task.rb class Task < ActiveRecord::Base belongs_to :assigned_user, :class => "User", :foreign_key => "assigned_user_id" validates_presence_of :description validates_presence_of :assigned_user_id # Validate against the foreign key, not the association end