DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

RSpec Testing :on => :create Validations

Mateusz Winiarski user avatar by
Mateusz Winiarski
·
Mar. 02, 09 · · Code Snippet
Like (0)
Save
Tweet
1.22K Views

Join the DZone community and get the full member experience.

Join For Free
ISSUE
Having class with validations, for example:


class User < ActiveRecord::Base
...
validates_format_of       :email, :with => /some_pattern/,
                          :on => :create
validates_length_of       :email, :within => 5..100,
                          :on => :create
validates_uniqueness_of   :email,
                          :on => :create
validates_confirmation_of :password,
                          :on => :create
...

all with :on => :create option, and trying to test it using RSpec in a common way, like this:

...
  it "should not be valid with wrong formated email" do
    @user.email = 'b@info'
    @user.should_not be_valid # wrong
  end
...
  it "should not be valid with duplicate user email" do
    @user2 = users(:aaron)
    @user.email = 'some@email.com'
    @user.save
    @user.should be_valid # it pass, but not sure of it
    @user2.email = @user.email
    @user2.save
    @user2.should_not be_valid # wrong
  end
...
  it "should not be valid without password confirmation" do
    @user = users(:user_no_pass)
    @user.password = 'sikret'
    @user.password_confirmation = nil
    @user.should_not be_valid # wrong
  end
...
  it "should not be valid with confirmation not matching password" do
    @user.password = 'password'
    @user.password_confirmation = 'not confirmed'
    @user.should_not be_valid # wrong
...

It all gives wrong output.


SOLUTION
Write a method and put it in a tested class, or in a ActiveRecord::Base, or maybe in a helper:


  def valid_on_create?
    errors.clear
    run_callbacks(:validate_on_create)
    validate_on_create
    errors.empty?
  end

Method valid_on_create? is taken from ActiveRecord::Base valid? method. (part of the code).

Now, you can write proper RSpec tests:

...
  it "should not be valid with wrong formated email" do
    @user.email = 'b@info'
    @user.should_not be_valid_on_create
  end
...
  it "should not be valid with duplicate user email" do
    @user2 = users(:aaron)
    @user.email = 'some@email.com'
    @user.save
    @user.should be_valid_on_create
    @user2.email = @user.email
    @user2.save
    @user2.should_not be_valid_on_create
  end
...
  it "should not be valid without password confirmation" do
    @user = users(:user_no_pass)
    @user.password = 'sikret'
    @user.password_confirmation = nil
    @user.should_not be_valid_on_create
  end
...
  it "should not be valid with confirmation not matching password" do
    @user.password = 'password'
    @user.password_confirmation = 'not confirmed'
    @user.should_not be_valid_on_create
...


NOTE: be_valid_on_create should be used only for validations with :on => :create option, otherwise it will give wrong results!!!

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Suspicious Sortings in Unity, ASP.NET Core, and More
  • Why to Implement GitOps into Your Kubernetes CI/CD Pipelines
  • Real-Time Supply Chain With Apache Kafka in the Food and Retail Industry
  • Querying Kafka Topics Using Presto

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo