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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:14:44+00:00 2026-05-24T13:14:44+00:00

I’m trying to create a test case for User model. Basically, it will validate

  • 0

I’m trying to create a test case for User model. Basically, it will validate first_name and last_name to be present.

What I am trying to do is to check whether the error on a specific field is empty or not and it should be empty. However it always fails.

What is the correct way to do this?

Here is my code

On my user_spec.rb

require 'spec_helper'

describe User do

  before do
    @user = User.new
  end

  it "must have a first name" do
    @user.errors[:first_name].should_not be_empty
  end

  it "must have a last name" do
    @user.errors[:last_name].should_not be_empty
  end
end

On my user.rb file

class User < ActiveRecord::Base
  validates :first_name, :presence => true
  validates :last_name, :presence => true
end
  • 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-24T13:14:45+00:00Added an answer on May 24, 2026 at 1:14 pm

    RSpec supports the notion of an “implicit” subject. If your first argument to the “describe” block is a class, RSpec automatically makes an instance of that class available to your specs. See http://relishapp.com/rspec/rspec-core/v/2-6/dir/subject/implicit-subject.

    require 'spec_helper'
    
    describe User do
    
      it "must have a first name" do    
        subject.should have(1).error_on(:first_name)
      end
    
      it "must have a last name" do
        subject.should have(1).error_on(:last_name)
      end
    end
    

    which results in RSpec output (if using –format documentation) of:

    User
      must have a first name
      must have a last name
    

    You can abbreviate it even further if you are content with the RSpec output defaults:

    require 'spec_helper'
    
    describe User do
      it { should have(1).error_on(:first_name) }
      it { should have(1).error_on(:last_name) }
    end
    

    which results in:

    User
      should have 1 error on :first_name
      should have 1 error on :last_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.