fluent-assertions

How to compare two Json objects using C#

耗尽温柔 提交于 2019-12-04 05:40:26
I have two Json objects as below need to be compared. I am using Newtonsoft libraries for Json parsing. string InstanceExpected = jsonExpected; string InstanceActual = jsonActual; var InstanceObjExpected = JObject.Parse(InstanceExpected); var InstanceObjActual = JObject.Parse(InstanceActual); And I am using Fluent Assertions to compare it. But the problem is Fluent assertion fails only when the attribute count/names are not matching. If the json values are different it passes. I require to fail when values are different. InstanceObjActual.Should().BeEquivalentTo(InstanceObjExpected); For

How to combine collection and property assertions using fluent-assertions?

不羁岁月 提交于 2019-12-04 02:52:28
I would like to "combine" Fluent Assertion's collection assertions and property assertions, e.g. assert that two IEnumerable 's are pairwise-equal using property-by-property (possibly "nested") comparison (i.e. structural equality, in functional language parlance). Concrete example: var dic = new Dictionary<int, string>() { {1, "hi"}, {2, "bye" } }; var actual = dic.ToSelectListItems(0).OrderBy(si => si.Text); var expected = new List<SelectListItem>() { new SelectListItem() {Selected = false, Text="bye", Value="2"}, new SelectListItem() {Selected = false, Text="hi", Value="1"} }; Here I wrote

How to use Exclude in FluentAssertions for property in collection?

烈酒焚心 提交于 2019-12-03 10:37:19
问题 I have two classes: public class ClassA { public int? ID {get; set;} public IEnumerable<ClassB> Children {get; set;} } public class ClassB { public int? ID {get; set;} public string Name {get; set;} } I want to use fluent assertions to compare to ClassA instances. However I want to ignore the IDs (because the IDs will have been assigned after the save). I know I can do this: expectedA.ShouldBeEquivalentTo(actualA, options => options.Excluding(x => x.PropertyPath == "Children[0].ID")); Which I

How to use Exclude in FluentAssertions for property in collection?

泄露秘密 提交于 2019-12-03 02:06:58
I have two classes: public class ClassA { public int? ID {get; set;} public IEnumerable<ClassB> Children {get; set;} } public class ClassB { public int? ID {get; set;} public string Name {get; set;} } I want to use fluent assertions to compare to ClassA instances. However I want to ignore the IDs (because the IDs will have been assigned after the save). I know I can do this: expectedA.ShouldBeEquivalentTo(actualA, options => options.Excluding(x => x.PropertyPath == "Children[0].ID")); Which I can obviously repeat for each ClassB in the collection. However I'm looking for a way to exclude the

How do I exclude a property of all items in IEnumerable when using ShouldBeEquivalentTo?

独自空忆成欢 提交于 2019-12-01 18:27:37
问题 In my NUnit/FluentAssertions tests I compare the complex object returned from my system with a reference one using the following code: response.ShouldBeEquivalentTo(reference, o => o.Excluding(x => x.OrderStatus) .Excluding(x => x.Id) .Excluding(x => x.Items[0].Name) .Excluding(x => x.Items[0].Article) .Excluding(x => x.ResponseStatus)); However, this is not exactly what I intended. I'd like to exclude Name and Article for every object in Items list and not only for the 0th. How do I

How do I exclude a property of all items in IEnumerable when using ShouldBeEquivalentTo?

▼魔方 西西 提交于 2019-12-01 17:54:48
In my NUnit/FluentAssertions tests I compare the complex object returned from my system with a reference one using the following code: response.ShouldBeEquivalentTo(reference, o => o.Excluding(x => x.OrderStatus) .Excluding(x => x.Id) .Excluding(x => x.Items[0].Name) .Excluding(x => x.Items[0].Article) .Excluding(x => x.ResponseStatus)); However, this is not exactly what I intended. I'd like to exclude Name and Article for every object in Items list and not only for the 0th. How do I implement this scenario? I've looked through the documentation and din't find the solution. Am I missing

Fluent Assertions: Compare two numeric collections approximately

纵然是瞬间 提交于 2019-12-01 17:18:26
问题 I have two arrays of double. Is there a way using FluentAssertions to compare the arrays element-by-element, using the .BeApproximately() technique? One range value would suffice for the entire array. Example: double[] source = { 10.01, 8.01, 6.01 }; double[] target = { 10.0, 8.0, 6.0 }; // THE FOLLOWING IS NOT IMPLEMENTED target.Should().BeApproximately(source, 0.01); Is there an alternative approach? 回答1: There's an overload on the generic collection assertions that takes a Func that you

Fluent Assertions: Approximately compare a classes properties

对着背影说爱祢 提交于 2019-12-01 15:43:33
I have a class Vector3D that has the properties X , Y and Z of type double (it also has other properties such as Magnitude ). What is the best way of approximately comparing all the properties or a selection of the properties at a given precision using Fluent Assertions? Currently I have been doing it like this: calculated.X.Should().BeApproximately(expected.X, precision); calculated.Y.Should().BeApproximately(expected.Y, precision); calculated.Z.Should().BeApproximately(expected.Z, precision); Is there a single line approach that will achieve the same thing? Such as using ShouldBeEquivalentTo

Can Fluent Assertions use a string-insensitive comparison for IEnumerable<string>?

混江龙づ霸主 提交于 2019-12-01 14:04:04
问题 I've got a pair of Lists I'm trying to compare using Fluent Assertions. I can code up a comparison easily, but I'd like to use Fluent Assertions so that I can get the reason to show up in the test failed message. Everything I've seen so far seems to using the default Object.Equals comparison, which is case-sensitive. I can't seem to pass an IComparer to the Equal or Contains methods, so is there any other way? [TestMethod()] public void foo() { var actual = new List<string> { "ONE", "TWO",

Fluent Assertions: Approximately compare a classes properties

夙愿已清 提交于 2019-11-30 22:45:41
问题 I have a class Vector3D that has the properties X , Y and Z of type double (it also has other properties such as Magnitude ). What is the best way of approximately comparing all the properties or a selection of the properties at a given precision using Fluent Assertions? Currently I have been doing it like this: calculated.X.Should().BeApproximately(expected.X, precision); calculated.Y.Should().BeApproximately(expected.Y, precision); calculated.Z.Should().BeApproximately(expected.Z, precision