2012-02-07 9 views
0

spec/models/season_spec.rbファイルにいくつかのRspecテストケースを記述しました。彼らは通りです: -Rspecユニットのテストケースが失敗しました

require 'spec_helper' 

describe Season do 

    it 'should not be without name' do 
    Season.new(:name=>nil,:number_of_weeks=>'3',:start_date=>'2012-02-07',:user_id=>'113').should_not be_valid 
    end 

    it 'should not be without number of weeks' do 
    Season.new(:name=>'Apurva',:number_of_weeks=>nil,:start_date=>'2012-02-07',:user_id=>'113').should_not be_valid 
    end 

    it 'should not be without start_date' do 
    Season.new(:name=>'Apurva',:number_of_weeks=>'3',:start_date=>nil,:user_id=>'113').should_not be_valid 
    end 

    it 'should not be without user_id' do 
    Season.new(:name=>'Apurva',:number_of_weeks=>'3',:start_date=>'2012-02-07',:user_id=>nil).should_not be_valid 
    end 

    it 'should be with valid attributes' do 
    Season.new(:name=>'Apurva',:number_of_weeks=>'3',:start_date=>'2012-02-07',:user_id=>'113').should be_valid 
    end 
end 

そして、私のモデルでは、私は、これらのフィールドを検証しています: -

class Season < ActiveRecord::Base 

    validates_presence_of :name,:number_of_weeks,:start_date,:user_id 
end 

しかし、まだテストケースは失敗しています。そして、それは私に出力を与えています: -

/usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:138: warning: Insecure world writable dir /usr/lib/ruby/gems/1.8 in PATH, mode 040777 
FFFFF 

Failures: 

/usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:165:in `pending_fixed?': undefined method `pending_fixed?' for #<ActiveRecord::StatementInvalid:0xb6cd03c8> (NoMethodError) 
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:19:in `dump_failures' 
    from /usr/lib/ruby/gems/1.8/bundler/gems/rails-27357a6965eb/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:473:in `each_with_index' 
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:17:in `each' 
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:17:in `each_with_index' 
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:17:in `dump_failures' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:75:in `send' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:75:in `notify' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:74:in `each' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:74:in `notify' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:23:in `conclude' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:14:in `report' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/command_line.rb:24:in `run' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:55:in `run_in_process' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:46:in `run' 
    from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:10:in `autorun' 
    from /usr/bin/rspec:4 
+0

ここに投稿するときは、コードとスタックトレースを4つのスペースでインデントしてフォーマットしてください。ありがとうございました。 –

+0

Ryanが示したようにフォーマットしてください - ありがとうございます。 –

+0

Mikhali、Ryan、Michaelありがとうございました。間違った形式をおかしなさい。今すぐ見てください –

答えて

2

最初に、'should not be without name'仕様に誤字があります。ここにquetionを入力している間、またはあなたのコードでこれがちょうどタイプミスかどうか確認してください。

第2に、これらのテストはすでにhereでテストされているため、無意味です。

+0

+1 Railsコードをテストしないようにアドバイスします。ビジネスロジックに重点を置く –

+0

@マークトーマス:あなたと私はここで間違っていると思います。 David Chelmisky氏は、検証の場合には一般的には明らかに正しい方法だと言います。仕様/テストを追加する必要があります。これは動作であるためです。彼のコメントはhttp://stackoverflow.com/questions/9175150/testing-the-relationships-and-methods-in-model-using-rspec#comment11545458_9175150 – Swanand

+0

で確認してください。検証はスペックとすることができますが、テストはビジネス要件を表す必要があります。検証システム自体をテストすることにラインを越えないように注意することの問題です。 –

関連する問題