如何在测试数据库中填充 table?
how filled table in test database?
请帮忙解决问题。
型号:
class StatusPoll < ActiveRecord::Base
has_many :polls
end
class Poll < ActiveRecord::Base
belongs_to :status_poll
end
spec/factories/status_poll.rb:
FactoryGirl.define do
factory :status_poll_0 do
id 0
title 'open'
end
factory :status_poll_1 do
id 1
title 'closed'
end
end
spec/factories/poll.rb:
FactoryGirl.define do
factory :poll do
association :status_poll_0
sequence(:title){ |i| "title#{i}" }
description Faker::Lorem.paragraph(7)
end
end
我需要填写 table 'status_poll' 以下值:
id: 0
title 'open'
id: 1
title: 'closed'
但是在控制台中的 运行 规格之后,我收到以下错误消息:
kalinin@kalinin ~/rails/phs $ rspec spec/models/poll_spec.rb
F...
Failures:
1) Poll has a valid factory
Failure/Error: expect(FactoryGirl.create(:poll)).to be_valid
NameError:
uninitialized constant StatusPoll0
uninitialized constant StatusPoll0
您收到此错误是因为您没有任何名为 StatusPoll0
的 class。相反,您有一个名为 StatusPoll
的 class。所以,你必须为你的工厂指定class。
在您的 spec/factories/status_poll.rb
中,指定工厂 class 如下:
FactoryGirl.define do
factory :status_poll_0, class: 'StatusPoll' do
id 0
title 'open'
end
factory :status_poll_1, class: 'StatusPoll' do
id 1
title 'closed'
end
end
这应该可以解决您的问题。
我不推荐您使用的方法。
相反,创建一个通用的 status_poll
工厂,如下所示:
FactoryGirl.define do
factory :status_poll do
end
end
然后在你的 poll
工厂中,像这样创建特征:
FactoryGirl.define do
factory :poll do
sequence(:title){ |i| "title#{i}" }
description Faker::Lorem.paragraph(7)
trait :poll_with_status_poll_0 do
after :build, :create do |poll|
status_poll = create :status_poll, id: 0, title: "open"
poll.status_poll = status_poll
end
end
trait :poll_with_status_poll_1 do
after :build, :create do |poll|
status_poll = create :status_poll, id: 1, title: "closed"
poll.status_poll = status_poll
end
end
end
end
现在在你的规范中,你可以像这样构建特征:
let(:poll_with_status_poll_0) { create :poll, :poll_with_status_poll_0 }
let(:poll_with_status_poll_1) { create :poll, :poll_with_status_poll_1 }
您真的想尝试为您的工厂建模以反映您的 rails 模型。在您的方法中,您生成的工厂模型并不代表您应用中的真实 类。
请帮忙解决问题。
型号:
class StatusPoll < ActiveRecord::Base
has_many :polls
end
class Poll < ActiveRecord::Base
belongs_to :status_poll
end
spec/factories/status_poll.rb:
FactoryGirl.define do
factory :status_poll_0 do
id 0
title 'open'
end
factory :status_poll_1 do
id 1
title 'closed'
end
end
spec/factories/poll.rb:
FactoryGirl.define do
factory :poll do
association :status_poll_0
sequence(:title){ |i| "title#{i}" }
description Faker::Lorem.paragraph(7)
end
end
我需要填写 table 'status_poll' 以下值:
id: 0
title 'open'
id: 1
title: 'closed'
但是在控制台中的 运行 规格之后,我收到以下错误消息:
kalinin@kalinin ~/rails/phs $ rspec spec/models/poll_spec.rb
F...
Failures:
1) Poll has a valid factory
Failure/Error: expect(FactoryGirl.create(:poll)).to be_valid
NameError:
uninitialized constant StatusPoll0
uninitialized constant StatusPoll0
您收到此错误是因为您没有任何名为 StatusPoll0
的 class。相反,您有一个名为 StatusPoll
的 class。所以,你必须为你的工厂指定class。
在您的 spec/factories/status_poll.rb
中,指定工厂 class 如下:
FactoryGirl.define do
factory :status_poll_0, class: 'StatusPoll' do
id 0
title 'open'
end
factory :status_poll_1, class: 'StatusPoll' do
id 1
title 'closed'
end
end
这应该可以解决您的问题。
我不推荐您使用的方法。
相反,创建一个通用的 status_poll
工厂,如下所示:
FactoryGirl.define do
factory :status_poll do
end
end
然后在你的 poll
工厂中,像这样创建特征:
FactoryGirl.define do
factory :poll do
sequence(:title){ |i| "title#{i}" }
description Faker::Lorem.paragraph(7)
trait :poll_with_status_poll_0 do
after :build, :create do |poll|
status_poll = create :status_poll, id: 0, title: "open"
poll.status_poll = status_poll
end
end
trait :poll_with_status_poll_1 do
after :build, :create do |poll|
status_poll = create :status_poll, id: 1, title: "closed"
poll.status_poll = status_poll
end
end
end
end
现在在你的规范中,你可以像这样构建特征:
let(:poll_with_status_poll_0) { create :poll, :poll_with_status_poll_0 }
let(:poll_with_status_poll_1) { create :poll, :poll_with_status_poll_1 }
您真的想尝试为您的工厂建模以反映您的 rails 模型。在您的方法中,您生成的工厂模型并不代表您应用中的真实 类。