2012-02-07 389 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

請在這裏發佈時,注意使用四個空格縮進來縮進代碼和堆棧跟蹤的格式。謝謝。 – 2012-02-07 10:08:52

+0

請格式化爲瑞恩表示 - 謝謝。 – 2012-02-07 10:14:11

+0

謝謝Mikhali,Ryan和Michael。對不起,格式錯誤。現在請看看 – 2012-02-07 10:28:16

回答

2

首先,在'should not be without name'規範中有一個錯字。請在這裏或在您的代碼中輸入quetion時,檢查這是否只是一個錯字。

其次,這些測試沒有意義,因爲代碼已經過測試here

+0

+1有關不測試Rails代碼的建議。專注於您的業務邏輯。 – 2012-02-07 13:02:51

+0

@MarkThomas:我認爲你和我在這裏可能是錯的。大衛·切爾米斯基說,總體來說,這顯然是正確的路要走,但是,在驗證的情況下 - 你需要添加規格/測試,因爲它是行爲。看看他的評論:http://stackoverflow.com/questions/9175150/testing-the-relationships-and-methods-in-model-using-rspec#comment11545458_9175150 – Swanand 2012-02-16 13:05:18

+0

同意。驗證可以是規格,但是測試應該代表業務需求。這只是注意不要跨越線路來測試驗證系統本身。 – 2012-02-16 13:19:19