4

私はRubyonRailsチュートリアルを進めており、終わりに近づいていますが、FactoryGirlはそれを簡単にしてくれません。

ファクトリを要求するスペックファイルを実行できません。

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

factorys.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'

また、FactoryGirlとの構文について、ここで提案されているsporkといくつかの組み合わせを再起動してみました。

ヘルプ!!!!

4

2 に答える 2

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
于 2012-11-16T18:08:52.940 に答える
0

問題は、ブロックを渡すことによってファクトリを宣言する方法にあると思います。micropostたとえば、次のようにファクトリを宣言してみてください。

factory :micropost do
    content "Foo bar"
    association :user
end
于 2012-11-16T18:02:59.053 に答える