How to test int equality with ShouldBe
Asked Answered
X

2

5

I am running c# tests using ShouldBe and I have this code:

int x = 3;
int y = 3;
x.ShouldBeSameAs(y);

Problem is it throws exception:

An exception of type 'Shouldly.ShouldAssertException' occurred in Shouldly.dll but was not handled in user code

Additional information: x

should be same as

3

but was

3

How can I test equality of to integers with ShouldBe?

Xerophyte answered 24/7, 2017 at 7:43 Comment(4)
I don't know what shouldly.dll is, but it seems to be comparing references. And since those references are not the same (x is not the same object as y) your assert fails. A quick look at the docs makes me wonder why you don;t use x.ShouldBe(y) in order to compare values.Ivory
@Xerophyte Why not Assert.AreEqual()?Skidway
To test if it is really reference equality that is being tested for, you could try int value = 3; object boxX = value; object boxY = boxX; boxX.ShouldBeSameAs(boxY);. Here boxX and boxY are reference to the same box (instance of a boxed value).Chromatism
@royalTS: Shouldly has nicer messages when condition is not satisfied, so it makes it easier to figure out what went wrong.Xerophyte
A
11

According to the documentation ShouldBeSameAs uses reference equality.

Use ShouldBe.

See documentation here.

Ataman answered 24/7, 2017 at 7:48 Comment(0)
B
5

Just use ShouldBe:

x.ShouldBe(y);
Blaise answered 24/7, 2017 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.