Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Faker gem

I installed Fabrication and Faker in my Rails 4 project

I created a fabrarication object:

Fabricator(:course) do
  title { Faker::Lorem.words(5) }
  description { Faker::Lorem.paragraph(2) } 
end

And I'm calling the Faker object within my courses_controller_spec.rb test:

require 'spec_helper'

describe CoursesController do
  describe "GET #show" do
    it "set @course" do
      course = Fabricate(:course)
      get :show, id: course.id
      expect(assigns(:course)).to eq(course)
    end
    it "renders the show template"
  end
end

But for some reason, the test failes at line 6:

course = Fabricate(:course)

the error message is:

Failure/Error: course = Fabricate(:course)
 TypeError:
   can't cast Array to string

Don't know exactly why this is failing. Has anyone experienced the same error message with Faker?

like image 560
Toontje Avatar asked Jan 12 '23 02:01

Toontje


1 Answers

The return from words(5) is an array, not a string. You should do this:

title { Faker::Lorem.words(5).join(" ") }
like image 55
Marcelo De Polli Avatar answered Jan 21 '23 07:01

Marcelo De Polli