我在使用 FactoryGirl 时遇到了这个问题。
每次我运行测试时,都会出现错误:未定义的方法 `to_i' for Array
我看不到它试图在哪里转换为整数。我最好的猜测是它试图将客户记录保存为数字,而不仅仅是保存记录的 ID。我尝试搜索文档以查看我是否错误地设置了工厂。
我db:test:prepare
希望是这样的。
你能看到哪里出了问题吗?
规格/工厂.rb
FactoryGirl.define do
factory :client do
name "Example"
email "email@example.com"
end
factory :book do
title "Book Title"
client_id {[FactoryGirl.create(:client)]}
end
end
规范/视图/书籍/index.html.erb_spec.rb
require 'spec_helper'
describe "books/index" do
before do
FactoryGirl.create(:book)
end
it "renders a list of books" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => "Title".to_s, :count => 2
assert_select "tr>td", :text => 1.to_s, :count => 2
end
end
测试输出
1) books/index renders a list of books
Failure/Error: FactoryGirl.create(:book)
NoMethodError:
undefined method `to_i' for #<Array:0x########>
# ./spec/views/books/index.html.erb_spec.rb:5:in `(root)'