Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7671425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:03:13+00:00 2026-05-31T16:03:13+00:00

I am newbie to Rails and trying to write application with TDD and BDD.

  • 0

I am newbie to Rails and trying to write application with TDD and BDD.

Now in one of the models there is a validation on length of a field. There is an RSpec having an example which checks length validation of this specific field.

Here is Model class

class Section < ActiveRecord::Base

    # Validations
    validates_presence_of :name, length: { maximum: 50 }

end

and RSpec

require 'spec_helper'

describe Section do
    before do
      @section = Section.new(name:'Test')
    end

    subject { @section }

    # Check for attribute accessor methods
    it { should respond_to(:name) }


    # Sanity check, verifying that the @section object is initially valid
    it { should be_valid }

    describe "when name is not present" do
        before { @section.name = "" }
        it { should_not be_valid }
    end

    describe "when name is too long" do
      before { @section.name = "a" * 52 }
      it { should_not be_valid }
    end
end

When I rung this spec example fails with following error

....F......

Failures:

  1) Section when name is too long 
     Failure/Error: it { should_not be_valid }
       expected valid? to return false, got true
     # ./spec/models/section_spec.rb:24:in `block (3 levels) in <top (required)>'

Finished in 0.17311 seconds
11 examples, 1 failure

Am I missing something here?

Also please suggest me some references to learn how Models should be tested especially relationship using RSpec (and Shoulda).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:03:14+00:00Added an answer on May 31, 2026 at 4:03 pm

    The validates_presence_of method does not have length option.
    You should validate length with validates_length_of method:

    class Section < ActiveRecord::Base
      # Validations
      validates_presence_of :name
      validates_length_of :name, maximum: 50
    end
    

    Or use rails3 new validation syntax:

    class Section < ActiveRecord::Base
      # Validations
      validates :name, presence: true, length: {maximum: 50}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I a newbie trying to write a [Ruby ON Rails] spec to make sure
I am a newbie to Rails and I have seen that to write HTML
I'm a newbie Rails developer who is getting the following error when trying to
I'm developing the Order Model for a Rails application.I'm trying to represent an Order
I am a complete newbie in working with a Ruby on Rails (RoR) application.
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.
I am a rails newbie and I am trying to create a database schema
I'm a rails newbie, I've been trying to figure out what is going on
Bit of a Rails newbie here so please bear with me. I am trying
hey I am newbie to ruby on rails and I am trying run a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.