Testing a Rails belongs_to model with Rspec
<p>I have a <code>Profile</code> model:</p>

<pre><code>class Profile &lt; ActiveRecord::Base
  belongs_to :user

  def fields_needed_for_completion
    [self.name, self.city]
  end

  def completed?
    !fields_needed_for_completion.any? { |f| f.nil? || f == "" }
  end
end
</code></pre>

<p>I am trying to determine how to write unit tests for a model with a <code>belongs_to</code> association.  In particular I'm not sure how to properly setup data for the tests.</p>

<p>So far, I've put together the following:</p>

<pre><code>describe Profile do

  subject(:profile) { FactoryGirl.create(:profile) }

  describe "fields_needed_for_completion" do
    context "with all fields missing" do
      it "returns all fields as nil" do
        expect(profile.fields_needed_for_completion.all? &amp;:blank?).to be true
      end
    end
  end

  describe "#completed?" do
    #TO DO
  end
end
</code></pre>

<p>Two questions:</p>

<ol>
<li><p>Is it fine to use FactoryGirl to create the Profile object instead of calling <code>Profile.create</code> directly?  There are no attributes set in the factory right now (i.e., the factory is defined like so: <code>factory :profile do; end</code>)</p></li>
<li><p>As you can see, the <code>User</code> model is not used at all in these specs.  Is it appropriate to unit test a model in isolation like this, even though in practice it will belong to a user? Or should I somehow mock out the User?</p></li>
</ol>