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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:21:06+00:00 2026-05-26T01:21:06+00:00

In solving exercise 5, I ran into a similar issue as this question .

  • 0

In solving exercise 5, I ran into a similar issue as this question. I refactored based on the answer given to that question but am still receiving the failure:

1) UsersController DELETE 'destroy' as an admin user should not self-destruct
     Failure/Error: lambda do
       count should have been changed by 0, but was changed by -1
    # ./spec/controllers/users_controller_spec.rb:354:in `block (4 levels) in <top     (required)>'

My spec:

  it "should destroy the user" do
    lambda do
      delete :destroy, :id => @user
    end.should change(User, :count).by(-1)
  end

  it "should redirect to the users page" do
    delete :destroy, :id => @user
    response.should redirect_to(users_path)
  end

  it "should not self-destruct" do
    lambda do
      delete :destroy, :id => @user.id
    end.should change(User, :count).by(0)
  end

and my controller:

def destroy
  @user = User.find(params[:id])
  if current_user == @user
    flash[:notice] = "You cannot destroy yourself"
  else
    @user.destroy
     flash[:success] = "User destroyed"  
  end
  redirect_to users_path
end

I’ve checked the behavior in the browser and it works as expected. As always, any help is appreciated. Thanks!


The updated, working code:

describe "as an admin user" do

  before(:each) do
    @admin = Factory(:user, :email => "admin@example.com", :admin => "true")
    test_sign_in(@admin)
  end

  it "should have links to destroy a user" do
    get :index
    response.should have_selector("a", :content => "delete" )
  end

  it "should destroy the user" do
    lambda do
      delete :destroy, :id => @user
    end.should change{ User.count }.by(-1)
  end

  it "should redirect to the users page" do
    delete :destroy, :id => @user
    response.should redirect_to(users_path)
  end

  it "should not be allowed to delete itself" do
    lambda do
      delete :destroy, :id => @admin
    end.should_not change{ User.count }
  end
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-26T01:21:06+00:00Added an answer on May 26, 2026 at 1:21 am

    I just realised that I read the wrong one of the three tests you posted (it would be clearer if you only posted the failing test 🙂

    However I’m confused, your "it should not self destruct" test is exactly the same as "it should destroy the user":

    it "should destroy the user" do
      lambda do
        delete :destroy, :id => @user
      end.should change(User, :count).by(-1)
    end
    
    it "should not self-destruct" do
      lambda do
        delete :destroy, :id => @user.id
      end.should change(User, :count).by(0)
    end
    

    You’re running the same test both times but expecting different outcomes. I would take a look again at the “it should not self destruct” and see what you mean by that (I’m not sure).

    I would guess that what needs to change is whatever is in the `change( xxxx ).by(0). What is it that shouldn’t be changing through the test?

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

Sidebar

Related Questions

Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
I was solving this problem6 , I dont even know the answer but, when
This is my first effort on solving the exercise. I gotta say, I'm kind
This is related to my previous question I'm solving UVA's Edit Step Ladders and
This question comes after solving my last question , I'd like to get some
I need your suggestions in solving this issue. Here is the requirement. We have
I'm solving this K&R exercise: Write versions of the library functions strncpy , strncat
While solving a DP related problem, I observed that first works but the second
I am stuck while solving this question, What is the minimum number of 2
Hello I am solving this task Problem28 , I think that its a easy

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.