question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The `Assert.AreEqual(object, object)` is very error prone and poor design

See original GitHub issue

Description

Due the fact that the Assert static class has an AreEqual(object, object) the compiler is always able to find a valid match during compilation if a type changes on one side of the assertion. This means all failures must be found very late in the game (read: after compilation and tests have been run).

This is generally not desirable. Either the AreEqual(object, object) method should go away, or a more strict version of AreEqual should introduced. An ideal API might be

void Equality<T>(T expected, T actual) where T: IEquatable; 
void Equality<T>(T expected, T actual, IEqualityComparer<T> comparer);
void SameObject(object expected, object actual); // performs ReferenceEquals(expected, actual)

With this triple alone, a test suite would cover 99% of equality tests without having to worry about the duck-typing mess we have today.

Steps to reproduce

Setup a test with Assert.AreEqual(0, obj.value) with obj.value starting as an int, then change the type to TimeSpan (or whatever). It’ll compile happily but fail when tests are run.

In a tiny project this is not a major issue, but in a large project where compilation take 45+ minutes and test runs can last an hour or more, this is a major time waster.

Expected behavior

.NET solutions not using duck-typing.

Actual behavior

MSTest uses duck-typing because … ?

Environment

Windows 10, Visual Studio 2017, NetFx 4.6.2, stock MSTest nuget packages.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
AbhitejJohncommented, Feb 6, 2020

Thanks folks. Would you mind giving the issue a 👍 and we’ll prioritize this. Sounds like we’d want to mark the API as obsolete for a few release so users are aware and remove it in the next major. /cc: @nohwnd

3reactions
Evangelinkcommented, Jul 12, 2022

We will add some analyzers that will prevent some issues on this. In parallel, we will make the object, object overload obsolete.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Assert.AreEqual(object, object) is very error prone and ...
This means all failures must be found very late in the game (read: after compilation and tests have been run).
Read more >
Assert.AreEqual fails while it shouldn't
Assert.AreEqual will simply check if the objects are equal, which will be done by calling expected.Equals(actual) .
Read more >
Comparing Two objects using Assert.AreEqual()
The reason is that deep down inside our assert have no idea what is an “equal” object and so it runs Object.Equals and...
Read more >
Assert.AreEqual in MSTest – done right
One of those so-called missing assertions is the one that enables a developer to compare two objects of the same type – by...
Read more >
Unit Testing and Coding: Why Testable Code Matters
Untestable code is a sign of deeper design problems. ... A system under test could be a method, a single object, or a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found