RSpec test with factory_girl and will_paginate for associated objects

做~自己de王妃 提交于 2019-12-11 05:58:25

问题


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                 "nisse@test.com"
  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


回答1:


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 use Factory.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 with Factory.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:

@owner.pets << Factory.create(:pet, :owner => @owner)

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.



来源:https://stackoverflow.com/questions/7692399/rspec-test-with-factory-girl-and-will-paginate-for-associated-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!