3

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?

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Uros
  • 2,099
  • 7
  • 23
  • 50
  • 1
    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. – oerkelens Jul 24 '17 at 07:48
  • @Uros Why not `Assert.AreEqual()`? – royalTS Jul 24 '17 at 08:14
  • 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). – Jeppe Stig Nielsen Jul 24 '17 at 08:27
  • @royalTS: Shouldly has nicer messages when condition is not satisfied, so it makes it easier to figure out what went wrong. – Uros Jul 24 '17 at 14:48

2 Answers2

9

According to the documentation ShouldBeSameAs uses reference equality.

Use ShouldBe.

See documentation here.

Joey
  • 344,408
  • 85
  • 689
  • 683
jProg2015
  • 1,098
  • 10
  • 40
4

Just use ShouldBe:

x.ShouldBe(y);
Joey
  • 344,408
  • 85
  • 689
  • 683