Created Issue: Output text of a collection assertion should be more friendly [12466]
Commented Issue: Output text of a collection assertion should be more friendly [12466]
Comments: Just realized that 2.0 improves support for this scenario with ShouldAllBeEquivalentTo() but I would like to see something more like ShouldAllHave().AllProperties().EqualTo()
New Post: Comparing two IEnumerables for Properties is not working
I have an IEnumerable of HappenedBusinessEvents and one of ExpectedBusinessEvents, and I want to assert that they both contain the same BusinessEvents by value, not by reference.
I tried it with HappenedBusinessEvents.ShouldHave().SharedProperties().IncludingNestedObjects().EqualTo(ExpectedBusinessEvents);
But it did not work. I have different texts in the 'description' string on both sides, but the test still passes. I have searched the forum but found no solution to my problem.
I would be glad for a hint.
Have a nice code,
Marco
New Post: Comparing two IEnumerables for Properties is not working
There is a new syntax for that. See http://www.dennisdoomen.net/2012/09/asserting-object-graph-equivalence.html
New Post: Comparing two IEnumerables for Properties is not working
Thank you,
I tried that, but it is not working. Let me provide a small codeexample:
public class DomainEvent
{
public readonly Guid ID;
public readonly string Info;
public DomainEvent(Guid iD, string info)
{
ID = Id;
Info = info;
}
}
[Test]
public void Testing_FluentAssertations()
{
var newID = Guid.NewGuid();
var given = new[] { new DomainEvent(newID(), info: "SomeInfo")};
var expect = new[] { new DomainEvent(newID(), info: "SomeInfo")};
given.ShouldAllBeEquivalentTo(expect);
}
It tells me, that the test fails with: "Expected item[0] to be DomainEvent info: SomeInfo., but found DomainEventt info: SomeInfo..
New Post: Comparing two IEnumerables for Properties is not working
The reason that this test is failing, lies in the fact that the DomainEvent class does not have any public properties. This causes the equivalency validation logic to treat it as a simple object, instead of a complex type. Complex types are compared, by looking at their properties, whereas simple objects are compared by reference. If you write your test like this, it will work:
[TestMethod] public void Testing_FluentAssertations() { var newID = Guid.NewGuid(); var given = new[] { new DomainEvent(newID, "SomeInfo") }; var expect = new[] { new DomainEvent(newID, "SomeInfo") }; given.ShouldAllBeEquivalentTo(expect); } internal class DomainEvent { public Guid ID { get; private set; } public string Info { get; private set; } public DomainEvent(Guid id, string info) { ID = id; Info = info; } }
New Post: Comparing two IEnumerables for Properties is not working
Thank you very much, of course that was it.
Sorry for the stupid question due to lack of attention on my side :)
Created Issue: add new string assertion .Should().BeInFormat(format, args) [12467]
Created Issue: BeInOrder does not cater for duplicate values [12468]
An array like this {1, 6 12, 12, 15 ,17, 26}
Reports that the item at index 2 is in the wrong order. Looking at the code I can see this is because the comparison of items is done with
int indexOfActualItem = Array.IndexOf(actualItems, orderedItem);
Which of course is going to return 2 for both instances of 12.
Created Issue: CollectionAssertions.HaveCount never calls Count property, always Enumerates [12469]
Gents,
public AndConstraint<TAssertions> HaveCount(int expected, string reason = "", params object[] reasonArgs)
{
...
int num = Enumerable.Count<object>(Enumerable.Cast<object>((IEnumerable) this.Subject));
...
}
is no good. In that call to Linq.Enumerable.Cast() --a function which isn't strictly a cast-- you're getting a yielded value from Subject, a type that doesn't actually relate to the original type of Subject. You cant use an uncasted Subject because Count requires a typed IEnumerable, not an untyped one.
I think the best solution is to do a couple things:
- Make HaveCount virtual.
- Have the non-generic one do the type checking to find the best method (either the property Count or manually enumerating via Count()).
- Have the GenericCollectionAssertions() override the Non-generic one and simply call Linq.Enumerable.Count() with an un-modified Subject.
So branch and pull request pending?
Commented Issue: CollectionAssertions.HaveCount never calls Count property, always Enumerates [12469]
Gents,
public AndConstraint<TAssertions> HaveCount(int expected, string reason = "", params object[] reasonArgs)
{
...
int num = Enumerable.Count<object>(Enumerable.Cast<object>((IEnumerable) this.Subject));
...
}
is no good. In that call to Linq.Enumerable.Cast() --a function which isn't strictly a cast-- you're getting a yielded value from Subject, a type that doesn't actually relate to the original type of Subject. You cant use an uncasted Subject because Count requires a typed IEnumerable, not an untyped one.
I think the best solution is to do a couple things:
- Make HaveCount virtual.
- Have the non-generic one do the type checking to find the best method (either the property Count or manually enumerating via Count()).
- Have the GenericCollectionAssertions() override the Non-generic one and simply call Linq.Enumerable.Count() with an un-modified Subject.
So branch and pull request pending?
Comments: ahh I have to have permissions to create a branch, that makes sense. I don't really want to fork the whole damn repo for something this small, should I just work off master? Presumably you wouldn't let me push. Sorry gents, new at this, can some one point me in the right direction? I'll work off a local branch in the mean time.
Commented Issue: CollectionAssertions.HaveCount never calls Count property, always Enumerates [12469]
Gents,
public AndConstraint<TAssertions> HaveCount(int expected, string reason = "", params object[] reasonArgs)
{
...
int num = Enumerable.Count<object>(Enumerable.Cast<object>((IEnumerable) this.Subject));
...
}
is no good. In that call to Linq.Enumerable.Cast() --a function which isn't strictly a cast-- you're getting a yielded value from Subject, a type that doesn't actually relate to the original type of Subject. You cant use an uncasted Subject because Count requires a typed IEnumerable, not an untyped one.
I think the best solution is to do a couple things:
- Make HaveCount virtual.
- Have the non-generic one do the type checking to find the best method (either the property Count or manually enumerating via Count()).
- Have the GenericCollectionAssertions() override the Non-generic one and simply call Linq.Enumerable.Count() with an un-modified Subject.
So branch and pull request pending?
Comments: Should().HaveCount(predicate) doesn't make any sense to me, shouldn't you be giving the predicate to Linq's Count()? What good is a transform on the integer?
Commented Issue: CollectionAssertions.HaveCount never calls Count property, always Enumerates [12469]
Gents,
public AndConstraint<TAssertions> HaveCount(int expected, string reason = "", params object[] reasonArgs)
{
...
int num = Enumerable.Count<object>(Enumerable.Cast<object>((IEnumerable) this.Subject));
...
}
is no good. In that call to Linq.Enumerable.Cast() --a function which isn't strictly a cast-- you're getting a yielded value from Subject, a type that doesn't actually relate to the original type of Subject. You cant use an uncasted Subject because Count requires a typed IEnumerable, not an untyped one.
I think the best solution is to do a couple things:
- Make HaveCount virtual.
- Have the non-generic one do the type checking to find the best method (either the property Count or manually enumerating via Count()).
- Have the GenericCollectionAssertions() override the Non-generic one and simply call Linq.Enumerable.Count() with an un-modified Subject.
So branch and pull request pending?
Comments: Alright, created a branch, throughly screwed up my git config but eventually got everything to where it needs to be. A little bit of background context, I'm working on some transactional memory stuff, and asking for the Count property is very different to a transaction than enumerating the whole list, so I'd really like fluent assertions to hit the property if it can.
New Post: name or description of the object being tested?
I'd like to specify the thing being tested?
For example, when this assertion fails
target.SystemCheckStatus.Should().Be(SystemCheckStatus.Warning);
the message is
"Expected object to be Warning, but found Ok."
I'd like the message to be
"Expected the status to be Warning, but found Ok."
or
"Expected systemCheckStatus to be Warning, but found Ok."
Is there a way to do this already?
Created Issue: Windows phone throwing non descript exceptions [12470]
[TestMethod]
public void InitialiseSession()
{
false.Should().BeTrue();
}
Results in :
New exception: "Could not find windows phone test framework.".
I'm using the latest test framework from toolkit (https://nuget.org/packages/WPToolkitTestFx)
Expected results: "True excepted ... "
Created Issue: FluentAssertions 2.0: objects, which are inherited from collections, are always compared as collections even that a comparison only by specified properties was requested. [12471]
Is this a bug? If not then how to compare similar objects only by specified properties?
Code to test the specified problem.
<pre>
using System;
using System.Collections.Generic;
using FluentAssertions;
using NUnit.Framework;
namespace FluentAssertionsTests
{
[TestFixture]
public class Tests
{
public class TestCollection : List<int>
{
public TestCollection()
{
Uid = Guid.NewGuid();
}
public int CollectionId { get; set; }
public Guid Uid { get; set; }
}
[Test]
public void DifferentCollectionsObjectGraphComparisonTest()
{
var actual = new TestCollection {CollectionId = 1};
actual.Add(1);
var expected = new TestCollection {CollectionId = 1};
expected.Add(2);
// this test failed with "Expected item[0] to be 2, but found 1."
actual.ShouldBeEquivalentTo(expected, options => options.Including(p => p.CollectionId));
}
[Test]
public void ObjectGraphComparisonTest()
{
var actual = new TestCollection {CollectionId = 1};
var expected = new TestCollection {CollectionId = 1};
// this test success
actual.ShouldBeEquivalentTo(expected, options => options.Including(p => p.CollectionId));
actual.Uid.Should().NotBe(expected.Uid);
// this test success but should be failed because objects has a different Uid
actual.ShouldBeEquivalentTo(expected, options => options.Including(p => p.Uid));
// this test success but should be failed because objects has a different Uid
actual.ShouldBeEquivalentTo(expected);
}
[Test]
public void DifferentCollectionsPropertyComparisonTest()
{
var actual = new TestCollection {CollectionId = 1};
actual.Add(1);
var expected = new TestCollection {CollectionId = 1};
expected.Add(2);
// this test failed with "Expected item[0] to be 2, but found 1."
actual.ShouldHave().Properties(p => p.CollectionId).EqualTo(expected);
}
[Test]
public void ObjectPropertyComparisonTest()
{
var actual = new TestCollection {CollectionId = 1};
var expected = new TestCollection {CollectionId = 1};
// this test success
actual.ShouldHave().Properties(p => p.CollectionId).EqualTo(expected);
actual.Uid.Should().NotBe(expected.Uid);
// this test success but should be failed because objects has a different Uid
actual.ShouldHave().Properties(p => p.Uid).EqualTo(expected);
// this test success but should be failed because objects has a different Uid
actual.ShouldHave().AllProperties().EqualTo(expected);
}
}
}
</pre>
Edited Issue: FluentAssertions 2.0: objects, which are inherited from collections, are always compared as collections even that a comparison only by specified properties was requested. [12471]
Created Issue: Extended reporting for ShouldBeEquivalentTo [12472]
Now we are using KellermanSoftware.CompareNetObjects for this purpose. This library is able to report a complete list of differences between two objects, but ideally we would like to have the same functionality directly in FluentAssertions. Is it possibly to implement such extended reporting for the ShouldBeEquivalentTo method?
Commented Issue: Windows phone throwing non descript exceptions [12470]
[TestMethod]
public void InitialiseSession()
{
false.Should().BeTrue();
}
Results in :
New exception: "Could not find windows phone test framework.".
I'm using the latest test framework from toolkit (https://nuget.org/packages/WPToolkitTestFx)
Expected results: "True excepted ... "
Comments: This happens when lib is used in windows phone 8 project. Updating the following class fixes the issue internal class WindowsPhoneTestFramework : LateBoundTestFramework { protected override string AssemblyName { get { return "Microsoft.VisualStudio.QualityTools.UnitTesting.Phone"; } } protected override string ExceptionFullName { get { return "Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException"; } } }
New Post: Throw custom exception on failed assertions
Hi, I like the library and I'm about to use it for objects validation in the production code, not in tests. Is it possible to set it to throw my own exception instead of the built-inAssertFailedException?