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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:52:21+00:00 2026-05-29T03:52:21+00:00

Busy writing a unit test for a controller that produces a view-model that includes

  • 0

Busy writing a unit test for a controller that produces a view-model that includes a list of options as IEnumerable < SelectListItem >. I tried checking that the expected list contains all the ones in the view-model and vice-versa. To my surprise this is always false. So I created the following test:

[TestMethod]
public void CanEqual()
{
  var x = new SelectListItem {Selected = false, Text = "A", Value = "A"};
  var y = new SelectListItem { Selected = false, Text = "A", Value = "A" };
  Assert.AreEqual(x, y); 
}

The assertion always fails but the two are equal. Does SelectListItem really not implement Equals or am I just missing something here?

  • 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-29T03:52:22+00:00Added an answer on May 29, 2026 at 3:52 am

    Adding to Shark’s answer… As for what to do about it, other than implementing IEquatable<T> on a derived class (and if you do that, you really should override the non-generic Equals() too – and if you do that, you really should override GetHashCode())… Anyway… other than doing that, you could:

    1. Create a helper method in your test project to do the value comparison (it’s possible to write a general purpose one using Reflection that would work for most simple classes), or
    2. Make a helper class implementing IEqualityComparer<T> for each of the types you need to compare.

    Neither will allow you to use Assert.AreEqual(), but in general, I’m not in favor of adding code to your objects just to allow testing – prefer keeping it in the test project. Plus, with these approaches, you won’t “need” to implement GetHashCode() etc.

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

Sidebar

Related Questions

I'm busy writing a class that monitors the status of RAS connections. I need
I'm busy writing a application that refreshes a IFrame completely using javascript, now my
I am busy writing a CodeSmith template that has one of its properties as
I'm busy writing a servlet that generates HTML code. The goal is to create
I am busy coding a unit test for an AJAX file uploader. The beginning
I am busy writing an application that runs under windows Where is the correct
I'm busy writing a PHP app that gets data from a database, but I
I'm busy writing a app that automacally defines a domain on a server and
Hl Guys, I am busy writing a backend administrative program for a system that
I am busy writing my thesis (so, I guess this could count as 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.