title: Building Model Associations in Factory Girl Tests
body: <p>Here is the relevant section from my models:</p>
  
  <pre><code>  belongs_to :cart
    belongs_to :product
    validate :quantity, :more_than_stock,  :message =&gt; "more than in stock is reserved." 
  
   def more_than_stock
      errors.add(:quantity, "should be less than in stock") if self.quantity &gt; self.product.stock
    end
  </code></pre>
  
  <p>I keep erroring out on this line: <code>errors.add(:quantity, "should be less than in stock") if self.quantity &gt; self.product.stock</code> with regards to the <code>.stock</code> method. </p>
  
  <p>The error I keep getting is: <code>1) Error:
  test_product_id_must_be_a_number(CartRowTest):
  NoMethodError: undefined method 'stock' for nil:NilClass</code> in my tests. </p>
  
  <p>It seems to me that my test suite doesn't know about the <code>.stock</code> method on product.</p>
  
  <p>However, here is my product factory:</p>
  
  <pre><code>factory :product do
      name 'Cholecap - 100mg'
      limit 3
      stock 10
    end
  </code></pre>
  
  <p>and my cart_row factory:</p>
  
  <pre><code> factory :cart_row do
      product
      cart
      quantity 3
    end
  </code></pre>
  
  <p>Here is the relevant portion of my unit test that throws the error:</p>
  
  <pre><code>def setup
      @cart_row = FactoryGirl.create(:cart_row)
    end
  
    test "product_id must not be blank" do
      @cart_row.product_id = "         "
      assert !@cart_row.valid?
    end
  
  test "product_id must be a number" do
       @cart_row.product_id = '234'
      assert !@cart_row.valid?
    end
  </code></pre>
  
  <p>What do I need to do to let the test suite know about the .stock method?</p>