FactoryGirl问题 - “工厂”:错误的参数数量(0表示1)(ArgumentError)

时间:2012-11-16 17:47:57

标签: ruby-on-rails ruby factory

我正在研究Ruby on Rails教程,我已经接近尾声了,但是Factory Girl对我来说并不容易。

我无法运行任何需要工厂的规范文件。

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

这是我收到的错误消息:

   /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'

我还尝试重新启动spork以及这里建议的一些组合,以获得FactoryGirl的语法。

帮助!

2 个答案:

答案 0 :(得分:1)

在工厂中创建对象时不需要循环,并且可以在同时定义对象时使用序列,试试这个:

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

答案 1 :(得分:0)

我认为问题在于你通过一个街区宣布工厂的方式。例如,尝试以这种方式声明micropost工厂:

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