kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Unit Testing open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Unit Testing

v1.11.0

v9.2.2

v4.1.0

Fixed typo in revisiting arrays and slices

4.1.0

googletest

v1.11.0

mocha

v9.2.2

ava

v4.1.0

learn-go-with-tests

Fixed typo in revisiting arrays and slices

jasmine

4.1.0

Popular Libraries in Unit Testing

Trending New libraries in Unit Testing

Top Authors in Unit Testing

1

19 Libraries

24123

2

11 Libraries

1657

3

10 Libraries

2759

4

10 Libraries

25001

5

9 Libraries

190

6

8 Libraries

5806

7

7 Libraries

311

8

7 Libraries

277

9

6 Libraries

21

10

6 Libraries

2241

1

19 Libraries

24123

2

11 Libraries

1657

3

10 Libraries

2759

4

10 Libraries

25001

5

9 Libraries

190

6

8 Libraries

5806

7

7 Libraries

311

8

7 Libraries

277

9

6 Libraries

21

10

6 Libraries

2241

Trending Kits in Unit Testing

Trending Discussions on Unit Testing

    assert that all fields in 2 objects are the same c#
    Skipping a method execution using Mockito
    How to locally unit-test Chainlink's Verifiable Random Function?
    How do I give my JSON schema an absolute URL for its $id when I haven't published it yet because it hasn't been tested yet?
    How to mock function without class in Kotlin?
    What is the 'X' in this insert > X'3C2F756C3E'
    Deploying Uniswap v2 / Sushiswap or similar in Brownie, Hardhat or Truffle test suite
    How to add unit testing in strapi version 4 using jest?
    What exactly is/means "System.IO.Stream+NullStream"?
    Unit testing SwiftUI/Combine @Published boolean values

QUESTION

assert that all fields in 2 objects are the same c#

Asked 2022-Apr-08 at 08:50

I am doing unit testing, and basically want to check that the data that 2 objects hold is the same

1Assert.AreEqual(object1, object2);   
2Assert.IsTrue(object1.Equals(object2)); //this of course doesn't work
3

I am searching for the C# equivalent of assertJ

1Assert.AreEqual(object1, object2);   
2Assert.IsTrue(object1.Equals(object2)); //this of course doesn't work
3Assert.That(object1).isEqualToComparingFieldByField(object2)
4

ANSWER

Answered 2022-Apr-08 at 07:47

You could either use records (c# 9 +) or you have to override the Equals method (if you have access and you can change the objects that you're working with). Records example:

copy icondownload icon

1Assert.AreEqual(object1, object2);   
2Assert.IsTrue(object1.Equals(object2)); //this of course doesn't work
3Assert.That(object1).isEqualToComparingFieldByField(object2)
4var point = new Point(3, 4);
5var point2 = new Point(3, 4);
6
7var test = point.Equals(point2); //this is true
8
9public record Point(int X, int Y);
10

with classes:

copy icondownload icon

1Assert.AreEqual(object1, object2);   
2Assert.IsTrue(object1.Equals(object2)); //this of course doesn't work
3Assert.That(object1).isEqualToComparingFieldByField(object2)
4var point = new Point(3, 4);
5var point2 = new Point(3, 4);
6
7var test = point.Equals(point2); //this is true
8
9public record Point(int X, int Y);
10public class Point
11{
12    public int X { get; }
13    public int Y { get; }
14
15
16    public override bool Equals(object? obj)
17    {
18        if (obj == null)
19         return false;
20
21        return obj is Point point && (point.X == X && point.Y == Y);
22    }
23
24    public override int GetHashCode()
25    {
26        return HashCode.Combine(X, Y);
27    }
28}
29

if you are not allowed to touch the implementation, then you could use serialization and compare the strings:

copy icondownload icon

1Assert.AreEqual(object1, object2);   
2Assert.IsTrue(object1.Equals(object2)); //this of course doesn't work
3Assert.That(object1).isEqualToComparingFieldByField(object2)
4var point = new Point(3, 4);
5var point2 = new Point(3, 4);
6
7var test = point.Equals(point2); //this is true
8
9public record Point(int X, int Y);
10public class Point
11{
12    public int X { get; }
13    public int Y { get; }
14
15
16    public override bool Equals(object? obj)
17    {
18        if (obj == null)
19         return false;
20
21        return obj is Point point && (point.X == X && point.Y == Y);
22    }
23
24    public override int GetHashCode()
25    {
26        return HashCode.Combine(X, Y);
27    }
28}
29var obj1Str = JsonConvert.SerializeObject(object1);
30var obj2Str = JsonConvert.SerializeObject(object2);
31Assert.Equal(obj1Str, obj2Str);
32

using Newtonsoft.Json nuget

Source https://stackoverflow.com/questions/71793327