2015-10-05 49 views
1

我運行rake db時總會得到以下錯誤:test:prepare。什麼會造成這種情況?耙db:創建工作正常。rake test prepare:NoMethodError:undefined方法`[]'爲零:NilClass

Adam-MacBook-Pro:katy adam$ bundle exec rake db:test:prepare 
    rake aborted! 
    NoMethodError: undefined method `[]' for nil:NilClass 
    /Users/adam/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:163:in `purge' 
    /Users/adam/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:356:in `block (3 levels) in <top (required)>' 
    /Users/adam/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:362:in `block (3 levels) in <top (required)>' 
    /Users/adam/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval' 
    /Users/adam/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>' 

的database.yml

<%= Rails.env %>: 
    adapter: mysql2 
    encoding: utf8 
    pool: <%= ENV['DB_POOL'] || 5 %> 
    username: <%= ENV['DB_USER'] || 'root' %> 
    password: <%= ENV['DB_PASSWORD'] || nil %> 
    timeout: <%= ENV['DB_TIMEOUT'] || 5000 %> 
    host: <%= ENV['DB_HOST'] || 'localhost' %> 
    port: <%= ENV['DB_PORT'] || 3306 %> 
    database: <%= ENV['DB_NAME'] || "website#{Rails.env}" %> 
+0

請問您可以發佈您的database.yml文件的內容? – Dusht

+0

好的,我已經更新了這個問題 – delpha

回答

2

嘗試指定RAILS_ENV當您運行rake命令:

RAILS_ENV=test bundle exec rake db:test:prepare 
+0

Thiks很多兄弟,但是爲什麼沒有'RAILS_ENV ='test''就沒有工作,據我所知,我沒有必要指定 – delpha

+1

顯然,對於該rake任務,RAILS_ENV'是'nil'。如果你已經有這樣的東西,你通常不需要指定'RAILS_ENV':'spec_helper.rb'文件中的'ENV ['RAILS_ENV'] || ='test''。 –

+0

因爲默認情況下,導軌在開發中啓動,而不是在測試中。 –

相關問題