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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:47:44+00:00 2026-05-20T03:47:44+00:00

Probably a simple answer that’s mock or stub related but I’m newish and trying

  • 0

Probably a simple answer that’s mock or stub related but I’m newish and trying to get a better understanding of things… I’m trying to understand why test resources aren’t actually deleted? but specs pass as if they were? This may (or may not) be unique to RSpec?!?

I have a new project using RSpec and generated a scaffold for a simple Resource

$ rails new destroyer -T
...
$ rails generate rspec:install
...
$ rails g scaffold resource name:string

Everything works, as expected, and specs pass. But I’ve had some curious stuff happen and tests fail in another project so digging deeper I added some logging to my destroy method to see what’s happening:

#app/controllers/resources_controller.rb
def destroy
  @resource = Resource.find(params[:id])
  @resource.destroy
  logger.info "Resource Destroyed: #{@resource.destroyed?}" # <-- added
  logger.info "Persisted after destroy: #{@resource.persisted?}" # <-- added

  respond_to do |format|
    format.html { redirect_to(resources_url) }
    format.xml  { head :ok }
  end
end

When I Destroy a resource I notice this (####) discrepancy in my logs:

development.log:

Started POST "/resources/3" for 127.0.0.1 at 2011-02-16 12:28:25 -0800
  Processing by ResourcesController#destroy as HTML
  Parameters: {"authenticity_token"=>"87+THlPY2Ni7vQCONbeSqwfoeXI2fesc7DIj6EMSaw=", "id"=>"3"}
  Resource Load (0.2ms)  SELECT "resources".* FROM "resources" WHERE "resources"."id" = 3 LIMIT 1
  AREL (0.5ms)  DELETE FROM "resources" WHERE "resources"."id" = 3
Resource Destroyed: true               # <<<<  ###### TRUE ######
Persisted after destroy: false
Redirected to http://localhost:4002/resources
Completed 302 Found in 30ms

and after running the specs I see this in test.log:

  Processing by ResourcesController#destroy as HTML
  Parameters: {"id"=>"1"}
Resource Destroyed: false              # <<<<  ###### FALSE ######
Persisted after destroy: false
Redirected to http://test.host/resources
Completed 302 Found in 4ms

Why are we seeing different states (destroyed?) of a resource depending on the environment we’re running in?

  • 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-20T03:47:44+00:00Added an answer on May 20, 2026 at 3:47 am

    If you are using the generated controller specs, you’ll see something like this for the destroy action:

    require 'spec_helper'
    
    describe PostsController do
    
      def mock_post(stubs={})
        @mock_post ||= mock_model(Post, stubs).as_null_object
      end
    
      it "destroys the requested post" do
        Post.stub(:find).with("37") { mock_post }
        mock_post.should_receive(:destroy)
        delete :destroy, :id => "37"
      end
    end
    

    The spec is not instantiating a real Post object, instead it is using mock_model to create a mock (or ‘test double’). To make it easier to test models, mocks created with mock_model stub out some of the ActiveRecord methods, including destroyed? and persisted?. As a result, these mocks won’t behave exactly the same way as real model instances.

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

Sidebar

Related Questions

I've got a question that probably has a pretty simple answer, but I didn't
This is probably a question that has an easy/simple/obvious answer, but I've found myself
There is probably a simple answer for this, but I couldn't find one, so
This is probably got a simple answer to it, but I am having problems
This is probably a simple one to answer, but I'm stuck, so here goes.
This is probably a really dumb question with a simple answer but... I am
( I know this is probably a simple question to answer, but I don't
I have a question that is probably very simple, but I cannot find an
This is probably quite simple but I've googled and can't find an answer. I
This is probably very simple, but I simply cannot find the answer myself :(

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.