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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:15:05+00:00 2026-06-05T04:15:05+00:00

I am fairly new to testing and have a UsersController that I want to

  • 0

I am fairly new to testing and have a UsersController that I want to test. I am starting with the new action and so far have the following;

require 'spec_helper'

describe UsersController do

  describe "GET 'new'" do
    it "assigns a new User to @user" do
      user = User.new
      get :new
      assigns(:user).should eq(user)
    end
    it "renders the :new template"
  end

end

My UsersController so far looks like this

class UsersController < ApplicationController
  def new
    @user = User.new
  end
end

I expected my first test to work but when I run it I get the following;

Failures:

  1) UsersController GET 'new' assigns a new User to @user
     Failure/Error: assigns(:user).should eq(user)

       expected: #<User id: nil, email: nil, username: nil, password_digest: nil, created_at: nil, updated_at: nil>
            got: #<User id: nil, email: nil, username: nil, password_digest: nil, created_at: nil, updated_at: nil>

       (compared using ==)

       Diff:#<User:0x007fe4bbfceed0>.==(#<User:0x007fe4bce5c290>) returned false even though the diff between #<User:0x007fe4bbfceed0> and #<User:0x007fe4bce5c290> is empty. Check the implementation of #<User:0x007fe4bbfceed0>.==.
     # ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

Playing around in the console reveals the following;

irb(main):001:0> a = User.new
=> #<User id: nil, email: nil, username: nil, password_digest: nil, created_at: nil, updated_at: nil>
irb(main):002:0> b = User.new
=> #<User id: nil, email: nil, username: nil, password_digest: nil, created_at: nil, updated_at: nil>
irb(main):003:0> a == b
=> false

So now I’m curious as to why 2 empty ActiveRecord objects are not equal (after all, Array.new == Array.new returns true), and what I have to do to make my test pass.

  • 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-05T04:15:07+00:00Added an answer on June 5, 2026 at 4:15 am

    You should probably change your test to something like this:

    describe UsersController do
    
      describe "GET 'new'" do
        it "assigns a new User to @user" do
          get :new
          assigns(:user).should be_new_record
          assigns(:user).kind_of?(User).should be_true
        end
        it "renders the :new template"   end
    
    end
    

    And you will have the same effect. If two objects aren’t saved, they don’t have primary keys to Rails is going to use object_id equality to compare them, that’s why they’re not ==.

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

Sidebar

Related Questions

I want to preface this question by stating that I am fairly new to
Im fairly new to unit testing and Jasmine. What I'm trying to test is
I'm fairly new to Dreamweaver and am testing the trial software. I have decent
I'm fairly new at Python/JS and also automated testing with Selenium/WebDriver, but I have
I'm implementing unit testing with PHPUnit on an existing codebase. I'm fairly new to
I fairly new to JQuery and perhaps trying to achieve something that might be
I am new to fairly new to AS3 and I have found myself needing
I am fairly new to C++ and I have seen a bunch of code
I am fairly new to Rails and I have never developed a large application.
I am fairly new to unit testing and am using VS2010 to develop in

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.