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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:52:25+00:00 2026-06-17T15:52:25+00:00

I am trying to refactor some RSpec/Rails tests so that they persist as few

  • 0

I am trying to refactor some RSpec/Rails tests so that they persist as few objects to the database as possible, but am having trouble trying to figure out how to re-write tests like the following:

describe User do
  context "record creation" do
    before(:each) { @user = User.new(user_atts) }

    it "should generate a confirmation_token" do
      # Generated as the result of a callback
      @user.save!
      expect(@user.confirmation_token).to be_present
    end

    it "should set the confirmed_at attribute to nil" do
      # Cleared as the result of a callback
      @user.save!
      expect(@user.confirmed_at).to be_nil
    end

    it "should call the send_confirmation_instructions method" do
      @user.should_receive(:send_confirmation_instructions) {}
      @user.save!
    end
  end

  def user_atts
    # return attributes hash
  end
end

This is a pretty simple example, but there are plenty of similar instances in my specs, and, for the most part, they all persist records to the database. I would love to take advantage of RSpec’s let and subject helpers, but am not fully sure that those would even help here.

I have been using FactoryGirl a lot and thought that maybe its build_stubbed strategy would speed up my specs a bit, but I couldn’t find many instances where it would help limit actual record creation (or maybe I don’t know how to use).

I assume there are some cases where a test requires record creation, but the above example hardly seems like one of them. Should I even be trying to refactor this or is there a better to write these tests? Any help would be greatly appreciated.

  • 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-06-17T15:52:25+00:00Added an answer on June 17, 2026 at 3:52 pm

    My tests would probably look something like this.

    describe User do
      let(:user) { FactoryGirl.build_stubbed(:user) } 
    
      context "record creation" do
        it "should generate a confirmation_token" do
          user.save!
          expect(user.confirmation_token).to be_present
        end
    
        it "should set the confirmed_at attribute to nil" do
          user.save!
          expect(user.confirmed_at).to be_nil
        end
    
        it "should call the send_confirmation_instructions method" do
          expect(user).to receive(:send_confirmation_instructions).once
          user.save!
        end
      end
    end
    

    That’s using Factory Girl to create the user models. Also, I’d have DatabaseCleaner to clear the database after each test as stated by @RahulGarg

    All you’d have to do is configure in your spec_helper something like this

      config.before(:suite) do
        DatabaseCleaner.strategy = :transaction
        DatabaseCleaner.clean_with(:truncation)
      end
    
      config.before(:each) do
        DatabaseCleaner.start
      end
    
      config.after(:each) do
        DatabaseCleaner.clean
      end
    

    This means after each test the Database would be cleared.

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

Sidebar

Related Questions

I'm trying to refactor some code but I'm kinda confused. I define my database
I'm working with Dynamics CRM 2011 Online and trying to refactor some code that
I'm trying to refactor some code here that was done previously by other guys,
I am trying to refactor some code I wrote that uses IndexedDb. Ideally what
So I'm trying to refactor some code which creates JDBC objects in a loop
I have some debugging functions that I would like to refactor, but seeing as
I have inherited some verbose, repetitious code that I am trying to refactor. The
Trying to refactor some code that has gotten really slow recently and I came
I have some linq code that I am trying to refactor because its not
I am trying to refactor some code in an ASP.Net website and having 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.