使用factory_girl和will_paginate对关联对象进行RSpec测试
我在 rspec 中为 will_paginate 编写测试时遇到问题。问题是我有一个模型,其主人可以养很多宠物。这会生成一个如下所示的工厂.rb 文件:
Factory.define :owner do |owner|
owner.personid "1111111"
owner.firstname "Nisse"
owner.lastname "Gunnarsson"
owner.street "Street"
owner.postaladress "38830"
owner.town "Town"
owner.phone "555-5555"
owner.mobile "555-5556"
owner.email "[email protected]"
owner.reminder true
end
Factory.define :pet do |pet|
pet.name "Hedvig"
pet.specie "Rabbot"
pet.breed "Lowen/vadur"
pet.colour "Madagaskar"
pet.association :owner
end
在我的测试中,我有
describe "Get show" do
before(:each) do
@owner = Factory(:owner)
30.times do
#@owner.pets << Factory.build(:pet)
@pet = Factory.build(:pet, :owner => @owner)
#@owner.pets << @pet
end
end
it "should have an element for each pet" do
get :show, :id => @owner
@owner.pets[0..2].each do |pet|
response.should have_selector("td", :content => pet.name)
end
response.should have_selector("td", :content => "Hedvig")
end
it "should paginate pets" do
get :show, :id => @owner
response.should have_selector("div.pagination")
response.should have_selector("span.disabled", :content => "Previous")
response.should have_selector("a", :href => "/pets?page=2",
:content => "2")
response.should have_selector("a", :href => "/pets?page=2",
:content => "Next")
end
end
所以我创建工厂的业主,没有问题。我可以通过 puts @owner.firstname
获取所有者姓名
我还可以创建一个拥有正确所有者 (@pet.owner.firstname
) 的宠物,但我可以不知道如何用宠物填充所有者数组(@owner.pets)。
如果我执行@owner.pets.count,它是0。
应用程序工作正常,我只是不知道如何编写测试。我对 Rails 和 TDD 都很陌生,但我想把它做好。
让我知道是否应该添加更多信息。
干杯卡尔
I am having problems with writing a test in rspec for will_paginate. The problem is that I have a model with an Owner that can have many pets. This leads to a factories.rb file that looks like this:
Factory.define :owner do |owner|
owner.personid "1111111"
owner.firstname "Nisse"
owner.lastname "Gunnarsson"
owner.street "Street"
owner.postaladress "38830"
owner.town "Town"
owner.phone "555-5555"
owner.mobile "555-5556"
owner.email "[email protected]"
owner.reminder true
end
Factory.define :pet do |pet|
pet.name "Hedvig"
pet.specie "Rabbot"
pet.breed "Lowen/vadur"
pet.colour "Madagaskar"
pet.association :owner
end
In my test I have
describe "Get show" do
before(:each) do
@owner = Factory(:owner)
30.times do
#@owner.pets << Factory.build(:pet)
@pet = Factory.build(:pet, :owner => @owner)
#@owner.pets << @pet
end
end
it "should have an element for each pet" do
get :show, :id => @owner
@owner.pets[0..2].each do |pet|
response.should have_selector("td", :content => pet.name)
end
response.should have_selector("td", :content => "Hedvig")
end
it "should paginate pets" do
get :show, :id => @owner
response.should have_selector("div.pagination")
response.should have_selector("span.disabled", :content => "Previous")
response.should have_selector("a", :href => "/pets?page=2",
:content => "2")
response.should have_selector("a", :href => "/pets?page=2",
:content => "Next")
end
end
So I create an Owner with the factory, no problem there. I can get the owners name by puts @owner.firstname
I can also create a pet, that has the correct owner (@pet.owner.firstname
), but I can not figure out how to fill the owners array (@owner.pets) with pets.
If I do a @owner.pets.count it is 0.
The applications works fine, I just can't figure out how to write the test. I am really new to both rails and TDD but I want to do it right.
Let me know if I should add more information.
Cheers Carl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,执行
@pet = Factory.build(:pet, :owner => @owner)
只会构建一个 Pet 对象,但不会将其保存到数据库中。您可能需要使用Factory.create(:pet, ...
来实际保存它。当您最初创建时,
@owner.pets
数组是 []如果您只是使用Factory.create
在数据库中创建记录,那么从技术上讲,@owner
有宠物,但是@owner
对象。不知道它们,因为它已经在内存中了 这不仅会将其保存到数据库,从而使对数据库的相反,尝试这样做:
任何新调用都有效(例如现在如果您执行了
Pet. count
你会得到 1),而且内存中的@owner.pets
数组中也会有一个有效的 Pet 对象。Well first, doing
@pet = Factory.build(:pet, :owner => @owner)
only builds a Pet object, but never saves it to the DB. You would want to useFactory.create(:pet, ...
to get it to actually save.The
@owner.pets
array is [] when you initially create the Owner object. If you simply create records in the DB withFactory.create
then yes, technically@owner
has pets, but the@owner
object doesn't know about them because it's already in memory with a.pets
array of [].Instead, try this:
That will not only save it to the database, thus making any new calls to the database valid (such as now if you did
Pet.count
you'd get back 1) but also the@owner.pets
array in memory will have a valid Pet object within it.