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 8139745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:55:51+00:00 2026-06-06T11:55:51+00:00

I guess the problem is that I do not know how to use factory

  • 0

I guess the problem is that I do not know how to use factory girl with Rspec correctly. Or testing in rails correctly for that matter. Still think it is a bit weird though..

I have a class, User, with the following factory:

FactoryGirl.define do
  factory :user do
    name     "admin"
    email    "admin@admin.com"
    adminstatus  "1"
    password "foobar"
    password_confirmation "foobar"
  end

  factory :user_no_admin, class: User do
    name     "user"
    email    "user@user.com"
    adminstatus  "2"
    password "foobar"
    password_confirmation "foobar"
  end
...

My test looks like this:

...
describe "signin as admin user" do
  before { visit login_path }
  describe "with valid information" do
    let(:user_no_admin) { FactoryGirl.create(:user_no_admin) }
    let(:user) { FactoryGirl.create(:user) }
    before do
      fill_in "User",    with: user.name
      fill_in "Password", with: user.password
      click_button "Login"
    end

    it "should list users if user is admin" do 
      response.should have_selector('th', content: 'Name') 
      response.should have_selector('td', content: user_no_admin.name)
      response.should have_selector('td', content: user.name)
    end
  end      
end#signin as admin user
...

Basically I am trying to test that if you log in as an admin, you should see a list of all the users. I have a test for logging on as a non-admin later on in the file. I have a couple of users in the db already.

In the list of users ‘admin’ that logged in is displayed along with the users already in the db. ‘user’ is however not displayed unless I do something like this before:

fill_in "User",    with: user_no_admin.name
fill_in "Password", with: user_no_admin.password

It is as if it won’t exist unless I use it. However, if I use a puts it does print the information I am putting, even if I do not do the ‘fill_in’ above.

I have a similar example where a puts helps me.

describe "should have company name" do
  let(:company) { FactoryGirl.create(:company) }
  let(:category) { FactoryGirl.create(:category) }
  let(:company_category) { FactoryGirl.create(:company_category, company_id: company.id, category_id: category.id) }
  it "should contain companies name" do
    puts company_category.category_id
    get 'categories/' + company.categories[0].id.to_s
    response.should have_selector('h4', :content => company.name)
  end
end

Without the puts above I get a

Called id for nil

Do I have to initiate(?) an object created by Factory girl before I can use it in some way?

Any other code needed?

  • 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-06-06T11:55:54+00:00Added an answer on June 6, 2026 at 11:55 am
    let(:whatever) 
    

    Is not creating the objects until the first time you call them. If you want it to be available before first use, use

    let!(:whatever)
    

    instead.

    Or use a before block:

    before(:each) do
      @company = FactoryGirl.create(:company)
      ....
    end
    

    Which will create the objects before you need to use them.

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

Sidebar

Related Questions

I know this looks embarrassingly easy, and I guess the problem is that I
I'm having a problem with getting an ArrayList to correctly use an overriden equals.
My Problem is the following. I got this code and i guess a corrupt
i am getting a problem while populating data inside the dropdown... I guess since
This is probably an easy one, but I'm missing something I guess. The problem
Guess that I have a TextView that I want to update it in some
I guess that the # Net column is the most interesting, although I don't
its been I while since I try to solve that problem, I've already asked
I know this is an oft-asked question/problem. I've looked through a bunch of Q&A
Ok, I think I know what is wrong, but I don't know how to

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.