加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 与Faker gem的问题

发布时间:2020-12-17 03:48:26 所属栏目:百科 来源:网络整理
导读:我在我的Rails 4项目中安装了Fabrication和Faker 我创建了一个fabrarication对象: Fabricator(:course) do title { Faker::Lorem.words(5) } description { Faker::Lorem.paragraph(2) } end 我在我的courses_controller_spec.rb测试中调用了Faker对象: re
我在我的Rails 4项目中安装了Fabrication和Faker

我创建了一个fabrarication对象:

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

我在我的courses_controller_spec.rb测试中调用了Faker对象:

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

但由于某种原因,测试在第6行失败:

course = Fabricate(:course)

错误消息是:

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

不知道为什么这会失败.有没有人遇到与Faker相同的错误消息?

解决方法

单词(5)的返回是一个数组,而不是一个字符串.你应该做这个:

title { Faker::Lorem.words(5).join(" ") }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读