fluent-assertions

ShouldBeEquivalentTo failing for equivalent objects when the subject is a DateTime

只愿长相守 提交于 2019-12-24 15:32:05
问题 What I'm trying to do I've just set up a test to ensure that a NodaTime LocalDateTime is mapped to a .NET DateTime , retaining the same date and time values. I'm using FluentAssertions' ShouldBeEquivalentTo method to compare the corresponding property values. [TestClass] public class LocalDateTimeMapping { [TestMethod] public void RetainsComponentValues() { var nodatimeTime = new LocalDateTime(); var dotnetTime = nodatimeTime.ToDateTimeUnspecified(); dotnetTime.ShouldBeEquivalentTo

FluentAssertions, making sure IEnumerable contains only single element

狂风中的少年 提交于 2019-12-24 05:42:51
问题 I am writing unit tests and I have something that looks like this: [Fact] public void GetFoos_only_gets_foo1() { _foo1.included = true; //Foo object _foo2.included = false; //Foo object _sut.GetFoos().Should().OnlyContain(_foo1); // this doesn't work, is there a specific syntax to use? } And GetFoos() returns and IEnumberable<Foo> 回答1: OnlyContain expects a predicate - GetFoos().Should().OnlyContain(x => _foo1.Equals(x)); 回答2: Try this page as see if it helps. https://github.com/dennisdoomen

Ignore internal properties in ShouldBeEquivalentTo

佐手、 提交于 2019-12-23 19:11:17
问题 Is there a way to ignore internal properties of a class when doing ShouldBeEquivalentTo? For example, in the class below I want to exclude the MetaData property from the object graph comparison. public class SomeObject { Public string SomeString { get; set; } internal MetaData MetaData { get; set; } } I would prefer to not use someObject.ShouldBeEquivalentTo(someOtherObject, options => options.Excluding(info => info.SelectedMemberPath == "MetaData") Because I might have more than 1 internal

How to check a list is ordered using Fluent Assertions

。_饼干妹妹 提交于 2019-12-23 14:21:48
问题 I am writing some unit tests using specflow and need a way to check whether a list of objects is ordered by a specific property. Currently I am doing it like this, but I am not sure if this is the best way to go about it. var listFromApi = listOfObjects; var sortedList = listFromApi.OrderBy(x => x.Property); Assert.IsTrue(listFromApi.SequenceEqual(sortedList)); Is there a nice way this can be done using Fluent Assertions? 回答1: Yes. You can use BeInAscendingOrder with a lambda. listFromApi

How to compare two Json objects using C#

ⅰ亾dé卋堺 提交于 2019-12-14 00:23:20
问题 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

Should FluentAssertions be used in production code? [closed]

偶尔善良 提交于 2019-12-11 09:15:52
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I've been using FluentAssertions for unit testing and was wondering why it's only ever mentioned in that context. If you generally write fail-fast production code with guards you would have to duplicate some of the functionality FluentAssertions already provides. Some of the

Using ShouldBeEquivalentTo and handling different names

自闭症网瘾萝莉.ら 提交于 2019-12-11 03:54:52
问题 I want to make a mapper test that maps a database model to a dto In the database model there is class Order { long Id } But on the Dto the same field is named class OrderDto { long OrderId } Using ShouldBeEquivalentTo how do I tell FluentAssertions that these fields are the same, but the name is different? 回答1: Its not exactly what you are asking for, but you can override the assertion comparison completely in fluent assertions for a given property with Using When. Its a little clunky and

FluentAssertions ShouldBeEquivalentTo() versus Should().BeEquivalentTo()

╄→гoц情女王★ 提交于 2019-12-08 19:09:48
问题 I have a test that verifies the collection output of a method. This variation of the test passes: [TestMethod, TestCategory("BVT")] public void TheStatusesAreReturned() { var expectedUnprocessedStatuses = new List<FileUploadStatus> { FileUploadStatus.InProcess, FileUploadStatus.Pending, }; Sut.GetUnprocessedStatuses() .Should() .BeEquivalentTo(expectedUnprocessedStatuses); } This variation of the test fails, with the error "Expected item[0] to be InProcess, but found Pending": [TestMethod,

C# Fluent Assertions global options for ShouldBeEquivalentTo

江枫思渺然 提交于 2019-12-08 14:46:47
问题 In Fluent Assertions when comparing objects with DateTime properties there are sometimes a slight mismatch in the milliseconds and the comparison fail. The way we get around it is to set the comparison option like so: actual.ShouldBeEquivalentTo(expected, options => options.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation)) .WhenTypeIs<DateTime>()); Is there a way to set this up once and have it always apply instead of having to specify it every time we call

How to compare two collections that vary by properties using Fluent Assertion?

回眸只為那壹抹淺笑 提交于 2019-12-07 21:56:28
问题 I have public class RuleInfo which is created from internal class Rule . private static RuleInfo CreateRuleInfo(Rule r) { return new RuleInfo { RuleCode = r.RuleId, DisplayName = r.RuleCode, Description = r.Description, LegacyRuleCode = null }; } They vary in their properties names so ShouldBeEquivalentTo() or ShouldAllBeEquivalentTo() don't work. Right now I'm comparing them manually/explicitly: foreach (var x in Enumerable.Zip(infs, rules, (i, r) => new { Info = i, Rule = r })) { x.Info