Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6647787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:34:44+00:00 2026-05-26T00:34:44+00:00

I am having problems with writing a test in rspec for will_paginate. The problem

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:34:44+00:00Added an answer on May 26, 2026 at 12:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem I'm having with writing a web application architecture is that I want
I am having a few problems re-writing flat links using htaccess, I have two
I am having a strange problem with a simple test app I am writing
I've been having problems writing XML and reading it in. I have a handwritten
I'm having problems reading/writing to stdin/stdout in a child process with subprocess.communicate(). This is
I am having some problems writing to a file in unicode inside my c
I'm writing a C program but I keep having problems with my array of
I'm having a problem writing Norwegian characters into an XML file using C#. I
I'm having a problem writing to a file: FileInputStream fin; try { fin =
I'm having a problem with getting a Ruby string substitution going. I'm writing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.