2017-08-09 27 views
0

我想測試我的應用程序,我不斷收到以下錯誤:解決活動記錄固定格式錯誤

Error: 
    ContractsControllerTest#test_should_get_show: 
    ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError 

Error: 
    ContractsControllerTest#test_should_get_show: 
    NoMethodError: undefined method `each' for nil:NilClass 

下面是我的合同控制器上的代碼,我也有類似這樣的代碼上的所有控制器。

def index 
     @contract = Contract.all.paginate(page: params[:page], :per_page => 70) 
    end 


    def show 
     @contract = Contract.find(params[:id]) 

    end 



    def new 
     @contract = Contract.new 
    end 


    def create 
     @contract = Contract.new(located) 
     if @contract.save 
      flash[:success] = "A record has been successfully added" 
     redirect_to contracts_path 
     else 
      render 'new' 
     end 
    end 



    def located 
     params.require(:contract).permit(:contract_name, :contract_status, :services_rendered, :contract_value, :award_year) 
    end 


     # editing a record in the contract from cotract table 
    def edit 
     @contract = Contract.find(params[:id]) 
    end 





     def update 
     @contract = Contract.find(params[:id]) 
     if @contract.update_attributes(located) 
      flash[:success] = "Contract form updated" 
      redirect_to contracts_path 
     else 
      render'edit' 
     end 
     end 



    def destroy 
     Contract.find(params[:id]).destroy 
     flash[:success] = "A record has been successfully deleted" 
     redirect_to contracts_path 
    end 

下面是錯誤的完整跟蹤時我運行命令導軌分貝:夾具:負載--trace

rails aborted! 
ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-  
5.0.5/lib/active_record/fixture_set/file.rb:72:in `validate' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixture_set/file.rb:49:in `raw_rows' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-  
5.0.5/lib/active_record/fixture_set/file.rb:37:in `config_row' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixture_set/file.rb:27:in `model_class' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:791:in `block (2 levels) in  
read_fixture_files' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixture_set/file.rb:15:in `open' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:790:in `block in read_fixture_files' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:789:in `each' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:789:in `each_with_object' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:789:in `read_fixture_files' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:598:in `initialize' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:529:in `new' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:529:in `block (2 levels) in  
create_fixtures' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:526:in `map' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:526:in `block in create_fixtures' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/connection_adapters/postgresql/referential_integrity.rb: 22:in `disable_referential_integrity' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:523:in `create_fixtures' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/railties/databases.rake:207:in `block (3 levels) in 
<top  (required)>' 
/usr/local/rvm/gems/ruby-2.3.0/gems/railties- 
5.0.5/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task' 
/usr/local/rvm/gems/ruby-2.3.0/gems/railties- 
5.0.5/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task' 
/usr/local/rvm/gems/ruby-2.3.0/gems/railties- 
5.0.5/lib/rails/commands/commands_tasks.rb:51:in `run_command!' 
/usr/local/rvm/gems/ruby-2.3.0/gems/railties- 
5.0.5/lib/rails/commands.rb:18:in `<top (required)>' 
/home/ubuntu/workspace/final_project/bin/rails:9:in `require' 
/home/ubuntu/workspace/final_project/bin/rails:9:in `<top (required)>' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring- 
2.0.2/lib/spring/client/rails.rb:28:in `load' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring- 
2.0.2/lib/spring/client/rails.rb:28:in `call' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring- 
2.0.2/lib/spring/client/command.rb:7:in `call' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:in  
`run' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/bin/spring:49:in `<top 
(required)>' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-  
2.0.2/lib/spring/binstub.rb:31:in `load' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring- 
2.0.2/lib/spring/binstub.rb:31:in`<top (required)>' 
/home/ubuntu/workspace/final_project/bin/spring:15:in `require' 
/home/ubuntu/workspace/final_project/bin/spring:15:in `<top (required)>' 
bin/rails:3:in `load' 
bin/rails:3:in `<main>' 
Tasks: TOP => db:fixtures:load 
(See full trace by running task with --trace) 

我的申請containts 12個控制器,在每個出現相同錯誤控制器。另外,我在我的模式中有10個表,其中一些表包含彼此的引用/關係。

我真的不知道什麼是錯,現在我已經爲這個問題掙扎了四天。如果有人能夠幫助我解決任何可能幫助我追蹤錯誤來源的代碼或信息,我將不勝感激。如果您能夠提供有關我應該將代碼放在哪裏以便跟蹤和解決此錯誤的信息,我也不勝感激。

謝謝

+0

我的回答是否幫助您解決問題?在StackOverflow上,您應該對收到的答案提供反饋,尤其是在他們幫助您的時候。如果答案解決了您的問題,那麼您應該將答案標記爲已接受,這將有助於其他具有相同問題的人找到解決方案。 – DRSE

+0

另外,我注意到你上週發佈了這個相同的問題三次,每個帖子都有細微的不同細節(這裏是https://stackoverflow.com/questions/45580331/active-record-fixture-format-error,這裏是https:/ /stackoverflow.com/questions/45473138/active-record-fixture-format-error),並沒有跟進他們中的任何一個。你不應該這樣做;相反,您應該更新原始帖子並提供更多詳細信息。這將把所有的問題信息保存在一個地方,以幫助你和那些試圖提供幫助的人。忽視他人的建議和努力,然後重新發布是不禮貌的。 – DRSE

+0

DRSE我很抱歉。事實上,我急於提交一個項目,但仍然是錯誤,我繼續得到錯誤,這就是爲什麼。 – fred

回答

1

其中一個夾具文件在測試/燈具中的格式不正確。這個問題似乎與您的控制器或測試無關,因爲它發生在每一個人身上。所有燈具在每次測試之前都會加載到測試數據庫中,即使其中一些未用於當前測試。因此,如果這些文件中有一個出現錯誤,那麼每次測試都會產生錯誤。

根據line from the activerecord source將錯誤提示到您提供的堆棧跟蹤頂部,ActiveRecord預計YAML夾具文件中的每個條目都是引用散列的鍵。

燈具中的每個條目應代表一個模型實例。頂級密鑰用作引用該實例的名稱(在生成新模型的燈具模板時,導軌通常將它們命名爲onetwo)。這裏有一個YAML夾具的例子,它有一個好的入口和幾個不同類型的壞的入口。

# This will produce a hash associated to key :hash_entry. 
# This is the correct type of entry, all others that follow are incorrect for rails fixtures. 
hash_entry: 
    key1: value1 
    key2: value2 

# This will produce the string "not a hash" associated to key :string_entry 
string_entry: not a hash 

# This will produce the array ["also", "not","a","hash"] associated to key :array_entry 
array_entry: 
    - also 
    - not 
    - a 
    - hash 

# This will produce nil associated to key :nil_entry 
nil_entry: 

你需要檢查你的燈具測試/夾具文件和尋找任何有壞的格式,如上所述。這是一個rake任務,可幫助您確定哪些文件和條目需要更正。首先運行rails g task fixtures check_format,並將此代碼放入在lib/tasks/fixtures.rake處生成的rake文件中。

namespace :fixtures do 
    desc "Looks for bad fixture files" 
    task check_format: :environment do 
    fixtures_dir = Rails.root.join("test", "fixtures") 
    fixture_files = Pathname.glob("#{fixtures_dir}/**/*.yml") 

    fixture_files.each do |file| 
     fixture = YAML.load(IO.read(file)) 

     fixture.each_pair do |name, entry| 
      puts "Bad fixture entry #{name}: #{entry.inspect} in fixture #{file}" unless entry.is_a? Hash 
     end 
    end 
    end 
end 

然後運行rails fixtures:check_format和有問題的文件和項目將被打印出來在命令行上爲您找到和正確的。