Last active
October 17, 2017 23:07
-
-
Save pjbgf/59f33303ce69e9721835f06a2b9ab316 to your computer and use it in GitHub Desktop.
Assertion Equality Code Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FluentAssertions; | |
using Xunit; | |
// To run this example you need to reference nuget packages: Xunit and FluentAssertions. | |
namespace EqualitySample | |
{ | |
public class EqualityTestShould | |
{ | |
[Fact] | |
public void Be_Equal_For_Value_Type() | |
{ | |
int value1 = 1; | |
int value2 = 1; | |
Assert.Equal(value1, value2); | |
} | |
[Fact] | |
public void Be_Equal_For_Array_Of_Value_Type() | |
{ | |
int [] value1 = { 1, 2, 3 }; | |
int [] value2 = { 1, 2, 3 }; | |
Assert.Equal(value1, value2); | |
} | |
[Fact] | |
public void Not_Be_Equal_For_Reference_Type() | |
{ | |
Customer customer1 = new Customer { Id = 1, Name = "John" }; | |
Customer customer2 = new Customer { Id = 1, Name = "John" }; | |
Assert.NotEqual(customer1, customer2); | |
} | |
[Fact] | |
public void Be_Equal_For_Same_Instance_Of_Reference_Type() | |
{ | |
Customer john = new Customer { Id = 1, Name = "John" }; | |
Assert.Equal(john, john); | |
Assert.Same(john, john); | |
} | |
[Fact] | |
public void Be_Equal_For_Reference_Type_When_Equals_Is_Overriden() | |
{ | |
CustomerBadExample customer1 = new CustomerBadExample { Id = 1, Name = "John" }; | |
CustomerBadExample customer2 = new CustomerBadExample { Id = 1, Name = "John" }; | |
Assert.Equal(customer1, customer2); | |
} | |
[Fact] | |
public void Be_Equal_For_Reference_Type_When_Using_FluentAssertions() | |
{ | |
Customer customer1 = new Customer { Id = 1, Name = "John" }; | |
Customer customer2 = new Customer { Id = 1, Name = "John" }; | |
customer1.ShouldBeEquivalentTo(customer2); | |
} | |
[Fact] | |
public void Be_Equal_For_Array_Of_Reference_Type_When_Using_FluentAssertions() | |
{ | |
var customers1 = new [] { new Customer { Id = 1, Name = "John" }, new Customer { Id = 2, Name = "Jack" } }; | |
var customers2 = new [] { new Customer { Id = 1, Name = "John" }, new Customer { Id = 2, Name = "Jack" } }; | |
customers1.ShouldBeEquivalentTo(customers2); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EqualitySample | |
{ | |
public class Customer | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
namespace EqualitySample | |
{ | |
public class CustomerBadExample | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public override bool Equals(object obj) | |
{ | |
if (!(obj is CustomerBadExample)) | |
return false; | |
var customer = obj as CustomerBadExample; | |
return (this.Id == customer.Id && | |
string.Compare(this.Name, customer.Name, true, CultureInfo.InvariantCulture) == 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment