mercredi 7 juin 2017

Why use factory_girl for rails testing?

I am a newbie at rails testing so i want to know the rationale behind using a gem called factory_girl for testing.

Currently i am only using rspecs. My model specs is as follows:

require 'rails_helper'

RSpec.describe User, type: :model do


  it 'creates user' do
      User.create(email: 'asd@we.com', password: 'asasdasdasd', admin: true, firstname: 'qwe', lastname: 'wer', grade: 5, section: 'w', role: 1)
      expect(User.count).to eq(1)
  end

  it 'should not have password attribute' do

      User.create(email: 'asd@we.com', password: 'asdasdasd', admin: true, firstname: 'qwe', lastname: 'wer', grade: 5, section: 'w', role: 1)
      expect(User.first.attributes).to_not include('password')

  end

  it 'encrypts password' do
      User.create(email: 'asd@we.com', password: 'asdasdasd', admin: true, firstname: 'qwe', lastname: 'wer', grade: 5, section: 'w', role: 1)
      expect(User.first.encrypted_password).to_not eq('asdasdasd')
  end




end

In the context of this code, i am expecting a rationale for integrating factory_girl for testing. Does using factory_girl make this code more concise and clear? What specific problems does factory_girl help solve? I appreciate any insights. Thanks!

Aucun commentaire:

Enregistrer un commentaire