0

当我运行 rspec spec/requests/users_spec.rb

我明白了:

admin@Administrators-MacBook-Pro:~/Desktop/sample_app$ rspec spec/requests/users_spec.rb
No DRb server is running. Running in local process instead ...
Rack::File headers parameter replaces cache_control after Rack 1.5.
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:6:in `define': wrong number of arguments (1 for 0) (ArgumentError)
    from /Users/admin/Desktop/sample_app/spec/factories.rb:4:in `<top (required)>'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:16:in `block in find_definitions'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/factory_girl_rails-4.1.0/lib/factory_girl_rails/railtie.rb:26:in `block in <class:Railtie>'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:34:in `call'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/admin/Desktop/sample_app/config/environment.rb:5:in `<top (required)>'
    from /Users/admin/Desktop/sample_app/spec/spec_helper.rb:8:in `require'
    from /Users/admin/Desktop/sample_app/spec/spec_helper.rb:8:in `block in <top (required)>'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork.rb:24:in `prefork'
    from /Users/admin/Desktop/sample_app/spec/spec_helper.rb:3:in `<top (required)>'
    from /Users/admin/Desktop/sample_app/spec/requests/users_spec.rb:1:in `require'
    from /Users/admin/Desktop/sample_app/spec/requests/users_spec.rb:1:in `<top (required)>'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:77:in `rescue in run'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:73:in `run'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'

这是 factory.rb:

# By using the symbol ':user', we get Factory Girl to simulate the User model.
# we can create a User factory in the tests like this:
# @user = Factory(:user)
FactoryGirl.define :user do |user|
    user.name "Pavan Katepalli"
    user.email "email@pavan.com"
    user.password "foobar"
    user.password_confirmation "foobar"
end

这是 users_spec:

require 'spec_helper'
require 'factory_girl_rails'

describe "Users" do

  describe "signup" do

        describe "failure" do
            it "should not make a new user" do
                lambda do
                    visit signup_path
                    fill_in "Name",         :with => ""
                    fill_in "Email",        :with => ""
                    fill_in "Password",     :with => ""
                    fill_in "Confirmation", :with => ""
                    click_button
                    response.should render_template('users/new')
                    response.should have_selector('div#error_explanation')
                end.should_not change(User, :count)
            end
        end

        describe "success" do
      it "should make a new user" do
        lambda do
          visit signup_path
          fill_in "Name",         :with => "Example User"
          fill_in "Email",        :with => "user@example.com"
          fill_in "Password",     :with => "foobar"
          fill_in "Confirmation", :with => "foobar"
          click_button
          response.should have_selector('div.flash.success',
                                        :content => "Welcome")
          response.should render_template('users/show')
        end.should change(User, :count).by(1)
      end
    end


    end

end

这是 users_controller_spec.rb:

require 'spec_helper'
require 'factory_girl_rails'

describe UsersController do

    render_views

    describe "GET 'show'" do

        before(:each) do
          @user = FactoryGirl(:user)
        end

        it "should be successful" do
          get :show, :id => @user.id
          response.should be_success
        end

        it "should find the right user" do
          get :show, :id => @user.id

          # assigns(:user) returns the 
          # value of the instance variable @user
          assigns(:user).should == @user
        end

        it "should have the right title" do
          get :show, :id => @user.id
          response.should have_selector('title', :content => @user.name)
        end

        it "should have the user's name" do
          get :show, :id => @user.id
          response.should have_selector('h1', :content => @user.name)
        end

        it "should have a profile image" do
          get :show, :id => @user.id
          response.should have_selector('h1>img', :class => "gravatar")
        end
    end

    describe "GET 'new'" do

        it "should be successful" do
          get :new
          response.should be_success
        end

        it "should have the right title" do
          get :new
          response.should have_selector('title', :content => "Sign up")
        end
    end

    describe "POST 'create'" do

        describe "failure" do

            before(:each) do
                @attr = { :name => "", :email => "", :password => "",
                      :password_confirmation => "" }
            end

            it "should not create a user" do
                lambda do
                    post :create, :user => @attr
                end.should_not change(User, :count)
            end

            it "should have the right title" do
                post :create, :user => @attr
                response.should have_selector('title', :content => "Sign up")
            end

            it "should render the 'new' page" do
                post :create, :user => @attr
                response.should render_template('new')
            end
        end

        describe "success" do

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

            it "should create a user" do
                lambda do
                    post :create, :user => @attr
                end.should change(User, :count).by(1)
            end

            it "should redirect to the user show page" do
                post :create, :user => @attr
                response.should redirect_to(user_path(assigns(:user)))
            end

            it "should have a welcome message" do
                post :create, :user => @attr
                flash[:success].should =~ /welcome to the sample app/i
            end

        end
    end



end
4

1 回答 1

1

您链接的存储库很旧(对 spec 文件夹的最后一次提交是 2 年前),并且您可能已经安装了使用的较新版本的 gem(包括 FactoryGirl)。

现在你可以做的是在你的rspec调用前加上bundle exec,这将在你当前包中的 gems 的上下文中运行 rspec:

bundle exec rspec spec/requests/users_spec.rb
于 2013-01-18T10:56:28.400 回答