加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – FactoryGirl问题 – “工厂”:错误的参数数量

发布时间:2020-12-17 02:03:26 所属栏目:百科 来源:网络整理
导读:我正在研究 Ruby on Rails教程,我已经接近尾声,但是Factory Girl对我来说并不容易. 我无法运行任何调用工厂的spec文件. require 'spec_helper'require 'factory_girl_rails'describe User do before(:each) do @attr = { :name = "Example User",:email = "u
我正在研究 Ruby on Rails教程,我已经接近尾声,但是Factory Girl对我来说并不容易.

我无法运行任何调用工厂的spec文件.

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,: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的语法.

救命!!!!

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读