2010-10-28 49 views
1

通過Michael Hartl的RailsTutorial工作,發現以下錯誤 - 即使我已將所有內容都記錄到了'T'。Rails3中未定義的名稱方法測試錯誤

1) UsersController GET 'index' for signed-in users should have an element for each user 
Failure/Error: response.should have_selector("li", :content => user.name) 
undefined method `name' for #<Array:0x000001032c07c8> 

沒有其他人得到類似的錯誤,並知道如何解決它?

我在第10章

順便說一句,當我嘗試在網頁它做什麼,這是應該做的。只是在RSpec中測試失敗。

僅供參考,這裏是從

require 'spec_helper' 

describe UsersController do 
    render_views 

    describe "GET 'index'" do 

     describe "for non-signed-in users" do 
      it "should deny access" do 
       get :index 
       response.should redirect_to(signin_path) 
       flash[:notice].should =~ /sign in/i 
      end 
     end 

     describe "for signed-in users" do 

      before(:each) do 
       @user = test_sign_in(Factory(:user)) 
       second = Factory(:user, :email => "[email protected]") 
       third = Factory(:user, :email => "[email protected]") 

       @users = [@user, second, third] 
      end 

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

      it "should have the right title" do 
       get :index 
       response.should have_selector("title", :content => "All users") 
      end 

      it "should have an element for each user" do 
       get :index 
       @users.each do |user| 
        response.should have_selector("li", :content => user.name) 
       end 
      end 
     end 
    end 

我的規格/ spec_helper.rb文件看起來像users_controller_spec.rb相關的測試代碼如下:

require 'rubygems' 
require 'spork' 

Spork.prefork do 
    # Loading more in this block will cause your tests to run faster. However, 
    # if you change any configuration or code from libraries loaded here, you'll 
    # need to restart spork for it take effect. 
    ENV["RAILS_ENV"] ||= 'test' 
    unless defined?(Rails) 
    require File.dirname(__FILE__) + "/../config/environment" 
    end 
    require 'rspec/rails' 

    # Requires supporting files with custom matchers and macros, etc, 
    # in ./support/ and its subdirectories. 
    Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} 

    Rspec.configure do |config| 
    # == Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 
    config.mock_with :rspec 

    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, comment the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    ### Part of a Spork hack. See http://bit.ly/arY19y 
    # Emulate initializer set_clear_dependencies_hook in 
    # railties/lib/rails/application/bootstrap.rb 
    ActiveSupport::Dependencies.clear 

    def test_sign_in(user) 
     controller.sign_in(user) 
    end 

    def integration_sign_in(user) 
     visit signin_path 
     fill_in :email,    :with => user.email 
     fill_in :password,   :with => user.password 
     click_button 
    end  
    end 
end 

Spork.each_run do 
end 

回答

0

看來你test_sign_in方法正在返回一個數組的實例而不是一個User對象。你是否明確地返回test_sign_in方法中的用戶對象?如果沒有,看看在該方法中執行的最後一行,我感覺它的結果是一個數組。

+0

這是test_sign_in'DEF test_sign_in(用戶) \t controller.sign_in(用戶)的定義 end' – marcamillion 2010-10-29 07:07:09

+0

在我看來,它返回一個普通對象,而不是陣列。鑑於我只是在學習,我可能會誤會......但我是否正確?順便說一句,我已更新我的帖子,以包含我的spec/spec_helper.rb文件的代碼,以防您可以在那裏找到任何線索。這就是我聲明test_sign_in方法的地方。 – marcamillion 2010-10-29 07:18:24

相關問題