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 6587199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:53:15+00:00 2026-05-25T16:53:15+00:00

I’m using rspec, rails, guard and sorcery for my authentication and testing. I have

  • 0

I’m using rspec, rails, guard and sorcery for my authentication and testing.

I have a test that is testing the length of an email. I want to reject emails that are too long. Here is the test I wrote for spec/models/user_spec.rb

    require 'spec_helper'

    describe User do
     before(:each) do
       @attr = { :email => "testuser@example.com", :password => "password", :password_confirmation => "password" }
     end


     it "should reject emails that are too long" do
       long_email = "a" * 101 + "gmail.com"
       long_email = User.new (@attr.merge(:email => long_email))
       long_email.should_not be_valid
     end

Here is the model validations I have in place:

class User < ActiveRecord::Base
  authenticates_with_sorcery!

  attr_accessible :email, :password, :password_confirmation

  validates_presence_of :password, :on => :create
  validates :password, :confirmation => true,
                       :length       => { :within => 6..100 }

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, :presence        => true,
                    :format          => { :with => email_regex },
                    :uniqueness      => {:case_sensitive => false},
                    :length          => { :within => 5..100 }
end

I’m a noob with this stuff, so any help would be greatly appreciated. The test is green right now, it goes red if I change the line to long_email.should be_valid. Thanks in advance!

  • 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-25T16:53:16+00:00Added an answer on May 25, 2026 at 4:53 pm

    As @apneadiving points out, the format of test is important. It not only makes it easier to read, but also shows up places you haven’t tested:

    require 'spec_helper'
    
    describe User do
      let(:attr){ 
        {:email => "testuser@example.com", :password => "password", :password_confirmation => "password" }
      }
    
      context "When given an email address" do
        context "That is too long" do
          let(:long_email){ "a" * 101 + "gmail.com" }
          subject{ User.new attr.merge(:email => long_email) }
    
          specify{ subject.should_not be_valid }
        end
        context "That is too short" do
          pending
        end
        context "That is between 5 and 100 characters long"
          pending
        end
      end
    
    end
    

    As you can see, rewriting it has done several things:

    • made it easier to read
    • made it easier to add more tests without having earlier tests impact later ones (by using let instead of before)
    • shown you that you’ve only specified a failing case, and not tested for the positive case (or other cases)

    If you write the positive case and that passes too then you really know something is wrong!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and

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.