0

I have written some Rspec test cases in my spec/models/season_spec.rb file. They are as:-

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

And in my model i have validated these fields as :-

class Season < ActiveRecord::Base

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

But still the test cases are failed. And it is giving me following output:-

/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
3
  • Please take care to format your code and stacktraces by indenting them with four spaces when posting here. Thank you. Commented Feb 7, 2012 at 10:08
  • Please format as Ryan has indicated - thanks. Commented Feb 7, 2012 at 10:14
  • Thank you Mikhali,Ryan and Michael. Sorry for the wrong format. Now please take a look Commented Feb 7, 2012 at 10:28

1 Answer 1

2

First, there is a typo in the 'should not be without name' spec. Please check if this is just a typo while typing the quetion here, or in your code.

Second, these tests are pointless since that code is already tested here.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 For advice to not test Rails code. Focus on your business logic.
@MarkThomas: I think you and I might be wrong here. David Chelmisky says that in general this is obviously the right way to go, however, in case of validations - you do need to add specs / tests, since it is behavior. Check out his comment at: stackoverflow.com/questions/9175150/…
Agreed. Validations can be specs, however the tests should be representative of the business requirements. It's just a matter of being careful not to cross the line into testing the validation system itself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.