I have a Profile model:
class Profile < 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
I am trying to determine how to write unit tests for a model with a belongs_to association. In particular I'm not sure how to properly setup data for the tests.
So far, I've put together the following:
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? &:blank?).to be true
end
end
end
describe "#completed?" do
#TO DO
end
end
Two questions:
-
Is it fine to use FactoryGirl to create the Profile object instead of calling
Profile.createdirectly? There are no attributes set in the factory right now (i.e., the factory is defined like so:factory :profile do; end) -
As you can see, the
Usermodel 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?
Aucun commentaire:
Enregistrer un commentaire