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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:39:08+00:00 2026-06-14T11:39:08+00:00

I am working through the Ruby on Rails tutorial and I am so close

  • 0

I am working through the Ruby on Rails tutorial and I am so close to the end, but Factory Girl is not making it easy for me.

I can’t run any of the spec files that call for factory.

require 'spec_helper'
require 'factory_girl_rails'



describe User do

 before(:each) do
    @attr = { :name => "Example User", 
                :email => "user@example.com",
                :password => "foobar",
                :password_confirmation => "foobar"
                }
    end

    it "should create a new instance given valid attributes" do
    User.create!(@attr)
    end

    it "should require a name" do
    no_name_user = User.new(@attr.merge(:name => ""))
    no_name_user.should_not be_valid
    end

    it "should require an email address" do
    no_email_user = User.new(@attr.merge(:email => ""))
    no_email_user.should_not be_valid
    end

    it "should reject names that are too long" do
    long_name = "a" * 51
    long_name_user = User.new(@attr.merge(:name => long_name))
    long_name_user.should_not be_valid
    end

    it "should accept valid email addresses" do
    addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
    addresses.each do |address|
        valid_email_user = User.new(@attr.merge(:email => address))
        valid_email_user.should be_valid
    end
    end

    it "should reject invalid email addresses" do
    addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
    addresses.each do |address|
        invalid_email_user = User.new(@attr.merge(:email => address))
        invalid_email_user.should_not be_valid
        end
        end

    it "should reject duplicate email addresses" do
    #put user with given email address into the database
        User.create!(@attr)
        user_with_duplicate_email = User.new(@attr)
        user_with_duplicate_email.should_not be_valid
    end

    it "should reject email addresses identical up to case" do
        upcased_email = @attr[:email].upcase
        User.create!(@attr.merge(:email => upcased_email))
        user_with_duplicate_email = User.new(@attr)
        user_with_duplicate_email.should_not be_valid
    end


    describe "password validations" do

        it "should require a password" do
            User.new(@attr.merge(:password => "", :password_confirmation => "")).should_not be_valid
            end

        it "should reject short passwords" do
        short = "a" * 5
        hash = @attr.merge(:password => short, :password_confirmation => short)
        User.new(hash).should_not be_valid
        end

        it "should reject long passwords" do
        long  = "a" * 41
        hash = @attr.merge(:password => long, :password_confirmation => long)
        User.new(hash).should_not be_valid
        end

    end

    describe "password encryption" do
        before(:each) do
            @user = User.create!(@attr)
    end
        it "should have an encrypted password attribute" do
        @user.should respond_to(:encrypted_password)
    end
        it "should set the encrypted password" do
        @user.encrypted_password.should_not be_blank
        end

    it "should be true if passwords match" do
        @user.has_password?(@attr[:password]).should be_true
    end

    it  "should be false if the passwords don't match" do
        @user.has_password?("invalid").should be_false
    end

    describe "authenticate method" do
        it "should return nil on email/password mismatch" do
        wrong_password_user = User.authenticate(@attr[:email], "wrongpass")
        wrong_password_user.should be_nil
        end

        it "should return nil for an email address with no user" do
        nonexistent_user = User.authenticate("bar@foo.com", @attr[:password])
        nonexistent_user_should be_nil
        end

        it "should return the user on email/password match" do
        matching_user = User.authenticate(@attr[:email], @attr[:password])
        matching_user.should == @user
        end
    end

    end 

    describe "admin attribute" do
        before(:each) do
            @user = User.create!(@attr)
        end

        it "should respond to admin" do
            @user.should respond_to(:admin)
        end

        it "should not be an admin by default" do
            @user.should_not be_admin
        end

        it "should be convertible to an admin" do
            @user.toggle!(:admin)
            @user.should be_admin
        end
    end

    describe "micropost associations" do
        before(:each) do
            @user = User.create{@attr}
            @mp1 = FactoryGirl.create(:micropost, :user => @user, :created_at => 1.day.ago)
            @mp2 = FactoryGirl.create(:micropost, :user => @user, :created_at => 1.hour.ago)

        end

        it "should have a microposts attribute" do
            @user.should respond_to(:microposts)
        end

        it "should have the right microposts in the right order" do
            @user.microposts.should == [@mp2, @mp1]
        end

        it "should destroy associated microposts" do
            @user.destroy
            [@mp1, @mp2].each do |micropost|
                Micropost.find_by_id(micropost.id).should be_nil
            end
        end     
    end 
end

factories.rb

    #by using the symbol ':user', we get Factory Girl to simmulate the User model
require 'spec_helper'

FactoryGirl.define do
    factory :user do |user|
        user.name                           "User Name"
        user.email                      "user@ex.com"
        user.password                   "foobar"
        user.password_confirmation  "foobar"
    end


    factory.sequence :email do |n|
        "person-#{n}@example.com"
    end

    factory :micropost do |micropost|
        micropost.content "Foo bar"
        micropost.association :user
    end
end

This is the error message I get:

   /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:15:in `factory': wrong number of arguments (0 for 1) (ArgumentError)
    from /Users/samanthacabral/rails_projects/sample/spec/factories.rb:13:in `block in <top (required)>'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `run'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:7:in `define'
    from /Users/samanthacabral/rails_projects/sample/spec/factories.rb:4:in `<top (required)>'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `block in load'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:16:in `block in find_definitions'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/factory_girl_rails-4.1.0/lib/factory_girl_rails/railtie.rb:26:in `block in <class:Railtie>'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:34:in `call'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/railties-3.2.8/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `run'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/railties-3.2.8/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `each'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/railties-3.2.8/lib/rails/application.rb:136:in `initialize!'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/railties-3.2.8/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/samanthacabral/rails_projects/sample/config/environment.rb:5:in `<top (required)>'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
    from /Users/samanthacabral/rails_projects/sample/spec/spec_helper.rb:81:in `<top (required)>'
    from /Users/samanthacabral/rails_projects/sample/spec/models/user_spec.rb:12:in `require'
    from /Users/samanthacabral/rails_projects/sample/spec/models/user_spec.rb:12:in `<top (required)>'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
    from /Users/samanthacabral/.rvm/gems/ruby-1.9.3-p286@rails3tutorial/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

I have also tried restarting spork and a few combinations suggested here for syntax with FactoryGirl.

HELP!!!!

  • 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-14T11:39:09+00:00Added an answer on June 14, 2026 at 11:39 am

    You don’t need to loop when create object in factory, and you can use sequence when define object same time, try this:

    FactoryGirl.define do  
        factory :user do
            sequence(:name) { |n| "User Name #{n}" }                         
            sequence(:email) { |n| "person-#{n}@example.com" } 
            password "foobar"
            password_confirmation "foobar"
        end
    
        factory :micropost do
            content "Foo bar"
            user
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working through the Ruby on Rails Tutorial. I've run into a problem
Hi i'm working through the Hartl's Ruby on Rails Tutorial and i'm stuck with
I am working through the Ruby on Rails 3 tutorial book and typed the
I am working through the Ruby on Rails Tutorial by Michael Hartl and I
I'm working through the Ruby on Rails Tutorial at http://railstutorial.org/ , chapter 4, listing
I'm working through the Ruby on Rails tutorial and just made a Comment model
I'm working through the Ruby on Rails 3 Tutorial: Learn Rails by Example by
I am working back through the Ruby on Rails 3 Tutorial and trying to
I am working through Michael Hartl's Ruby on Rails Tutorial Chapter 5 exercises and
I am working through M. Hartl's Ruby on Rails Tutorial screen casts, which involve

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.