0

So how we resolve the error in initialize function,Which thing need to be done to remove this error

Here is the Factories/user.rb

FactoryGirl.define do
  factory :user do |u|
    u.first_name Faker::Name.first_name
    u.last_name Faker::Name.last_name
    u.email Faker::Internet.email
    u.username Faker::Name.name
    u.title Faker::Name.title
    u.password "hellohello"
    u.password_confirmation "hellohello"
 end
end

Here is the mode spec model/user_spec.rb

require 'rails_helper'

RSpec.describe User, :type => :model do

  user = ''
  describe "create" do
    user = FactoryGirl.create(:user)
  end
  #describe "validations"
  describe "Count all the user" do
    it "should have one item created before being created" do
      #binding.pry
      expect(User.all.count).to eq(1)
    end
 end
end 

When testing the model user_spec.rb model it gives the error: enter image description here

Scimonster
  • 32,893
  • 9
  • 77
  • 89
Aqib Hameed
  • 77
  • 1
  • 9

1 Answers1

0

Change this:

u.first_name Faker::Name.first_name
u.last_name Faker::Name.last_name
u.email Faker::Internet.email
u.username Faker::Name.name
u.title Faker::Name.title

To this:

u.first_name {Faker::Name.first_name}
u.last_name {Faker::Name.last_name}
u.email {Faker::Internet.email}
u.username {Faker::Name.name}
u.title {Faker::Name.title}

This will cause Ruby to reevaluate the calls to Faker each time, generating various values each time.

Note that there is still the possibility for collisions, as Faker::email could generate the same email each time. I'd suggest using a sequence instead for fields with unique constraints. Something like this:

   sequence(:email) { |n| "person#{n}@example.com" }
   sequence(:username, 1000) { |n| "user#{n}" }
David Hempy
  • 5,373
  • 2
  • 40
  • 68
  • 1
    I use sequence instead of faker i got the same issue Failure/Error: def initialize @example = nil end **ArgumentError:wrong number of arguments (1 for 0)** – Aqib Hameed Dec 22 '16 at 07:41