Quantcast
Channel: Fluent Assertions
Viewing all 1402 articles
Browse latest View live

Edited Unassigned: NullReferenceException caused by AttributeBasedFormatter on Mono 3.0.6 [12487]

$
0
0
```
(true).Should().BeFalse();
```

results in

```
System.NullReferenceException : Object reference not set to an instance of an object

at FluentAssertions.Formatting.AttributeBasedFormatter.<FindCustomFormatters>b__4 (System.Type type) [0x00000] in <filename unknown>:0
at System.Linq.Enumerable+<CreateSelectManyIterator>c__Iterator29`3[System.Type,System.Reflection.MethodInfo,<>f__AnonymousType0`2[System.Type,System.Reflection.MethodInfo]].MoveNext () [0x00059] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-3.0.6/_build/mono-3.0.6.git/mcs/class/System.Core/System.Linq/Enumerable.cs:2335
```

Obviously, AttributeBasedFormatter.GetExportedTypes(Assembly) sometimes returns an array that contains null references in Mono, because a ReflectionTypeLoadException is thrown whose Types property contains some null values.

We can easily fix that in AttributeBaseFormatter.FindCustomFormatters():

```
IEnumerable<MethodInfo> source =
from type in AttributeBasedFormatter.AllTypes
where type != null // FIX
from method in type.GetMethods(24)
where method.get_IsStatic()
where method.HasAttribute<ValueFormatterAttribute>()
where method.GetParameters().Count<ParameterInfo>() == 1
select method;
return source.ToArray<MethodInfo>();
```



Commented Unassigned: NullReferenceException caused by AttributeBasedFormatter on Mono 3.0.6 [12487]

$
0
0
```
(true).Should().BeFalse();
```

results in

```
System.NullReferenceException : Object reference not set to an instance of an object

at FluentAssertions.Formatting.AttributeBasedFormatter.<FindCustomFormatters>b__4 (System.Type type) [0x00000] in <filename unknown>:0
at System.Linq.Enumerable+<CreateSelectManyIterator>c__Iterator29`3[System.Type,System.Reflection.MethodInfo,<>f__AnonymousType0`2[System.Type,System.Reflection.MethodInfo]].MoveNext () [0x00059] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-3.0.6/_build/mono-3.0.6.git/mcs/class/System.Core/System.Linq/Enumerable.cs:2335
```

Obviously, AttributeBasedFormatter.GetExportedTypes(Assembly) sometimes returns an array that contains null references in Mono, because a ReflectionTypeLoadException is thrown whose Types property contains some null values.

We can easily fix that in AttributeBaseFormatter.FindCustomFormatters():

```
IEnumerable<MethodInfo> source =
from type in AttributeBasedFormatter.AllTypes
where type != null // FIX
from method in type.GetMethods(24)
where method.get_IsStatic()
where method.HasAttribute<ValueFormatterAttribute>()
where method.GetParameters().Count<ParameterInfo>() == 1
select method;
return source.ToArray<MethodInfo>();
```


Comments: Was already fixed in commit e4911756

Edited Issue: Misleading AllDeclaredPublicPropertiesSelectionRule class [12486]

$
0
0
I would expect this class to provide a list of public properties only. However under the hood, in the SelectProperties method, it is calling GetNonPrivateProperties which can return protected, internal & public properties.






Commented Issue: Misleading AllDeclaredPublicPropertiesSelectionRule class [12486]

$
0
0
I would expect this class to provide a list of public properties only. However under the hood, in the SelectProperties method, it is calling GetNonPrivateProperties which can return protected, internal & public properties.






Comments: Fixed in commit https://fluentassertions.codeplex.com/SourceControl/changeset/e0d5f9305f298d7d001180f4399973b1b5327c21. However, internal properties are still included. If the test project has access to those (through an [InternalsVisibleTo] attribute), then we treat that as 'public'.

Edited Issue: ShouldBeEquivalentTo crashes on signature hiding properties [12481]

$
0
0
Take this example. Foo is overriden with new. This crashes TypeExtensions.FindProperty because it will see object Foo and string Foo

```c#
[TestMethod]
public void FluentAssertionsTest()
{
var a1 = new B<string> { Foo = "test" };
var a2 = new E<string> { Foo = "test" };

a1.ShouldBeEquivalentTo(a2);
//a1.ShouldBeEquivalentTo(a2, ex => ex.Using(new MustMatchByNameAndTypeRule()));
}

public class A
{
public object Foo { get; set; }
}

public class B<T> : A
{
public new T Foo
{
get
{
return (T)base.Foo;
}

set
{
base.Foo = value;
}
}
}

public class D
{
public object Foo { get; set; }
}

public class E<T> : D
{
public new T Foo
{
get
{
return (T)base.Foo;
}

set
{
base.Foo = value;
}
}
}
```

My suggestion is to add 2 new matchers:
```c#
public class MustMatchByNameAndTypeRule : IMatchingRule
{
public PropertyInfo Match(PropertyInfo subjectProperty, object expectation, string propertyPath)
{
PropertyInfo compareeProperty = expectation.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.SingleOrDefault(pi => pi.Name == subjectProperty.Name && pi.PropertyType == subjectProperty.PropertyType);

if (compareeProperty == null)
{
string path = (propertyPath.Length > 0) ? propertyPath + "." : "property ";

Execute.Verification.FailWith(
"Subject has " + path + subjectProperty.Name + " that the other object does not have.");
}

return compareeProperty;
}

public override string ToString()
{
return "Match property by name and type (or throw)";
}
}

public class TryMatchByNameAndTypeRule : IMatchingRule
{
public PropertyInfo Match(PropertyInfo subjectProperty, object expectation, string propertyPath)
{
PropertyInfo compareeProperty = expectation.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.SingleOrDefault(pi => pi.Name == subjectProperty.Name && pi.PropertyType == subjectProperty.PropertyType);

return compareeProperty;
}

public override string ToString()
{
return "Try to match property by name and type";
}
}
```

Edited Issue: ShouldBeEquivalentTo crashes on signature hiding properties [12481]

$
0
0
Take this example. Foo is overriden with new. This crashes TypeExtensions.FindProperty because it will see object Foo and string Foo

```c#
[TestMethod]
public void FluentAssertionsTest()
{
var a1 = new B<string> { Foo = "test" };
var a2 = new E<string> { Foo = "test" };

a1.ShouldBeEquivalentTo(a2);
//a1.ShouldBeEquivalentTo(a2, ex => ex.Using(new MustMatchByNameAndTypeRule()));
}

public class A
{
public object Foo { get; set; }
}

public class B<T> : A
{
public new T Foo
{
get
{
return (T)base.Foo;
}

set
{
base.Foo = value;
}
}
}

public class D
{
public object Foo { get; set; }
}

public class E<T> : D
{
public new T Foo
{
get
{
return (T)base.Foo;
}

set
{
base.Foo = value;
}
}
}
```

My suggestion is to add 2 new matchers:
```c#
public class MustMatchByNameAndTypeRule : IMatchingRule
{
public PropertyInfo Match(PropertyInfo subjectProperty, object expectation, string propertyPath)
{
PropertyInfo compareeProperty = expectation.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.SingleOrDefault(pi => pi.Name == subjectProperty.Name && pi.PropertyType == subjectProperty.PropertyType);

if (compareeProperty == null)
{
string path = (propertyPath.Length > 0) ? propertyPath + "." : "property ";

Execute.Verification.FailWith(
"Subject has " + path + subjectProperty.Name + " that the other object does not have.");
}

return compareeProperty;
}

public override string ToString()
{
return "Match property by name and type (or throw)";
}
}

public class TryMatchByNameAndTypeRule : IMatchingRule
{
public PropertyInfo Match(PropertyInfo subjectProperty, object expectation, string propertyPath)
{
PropertyInfo compareeProperty = expectation.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.SingleOrDefault(pi => pi.Name == subjectProperty.Name && pi.PropertyType == subjectProperty.PropertyType);

return compareeProperty;
}

public override string ToString()
{
return "Try to match property by name and type";
}
}
```

Commented Issue: ShouldBeEquivalentTo crashes on signature hiding properties [12481]

$
0
0
Take this example. Foo is overriden with new. This crashes TypeExtensions.FindProperty because it will see object Foo and string Foo

```c#
[TestMethod]
public void FluentAssertionsTest()
{
var a1 = new B<string> { Foo = "test" };
var a2 = new E<string> { Foo = "test" };

a1.ShouldBeEquivalentTo(a2);
//a1.ShouldBeEquivalentTo(a2, ex => ex.Using(new MustMatchByNameAndTypeRule()));
}

public class A
{
public object Foo { get; set; }
}

public class B<T> : A
{
public new T Foo
{
get
{
return (T)base.Foo;
}

set
{
base.Foo = value;
}
}
}

public class D
{
public object Foo { get; set; }
}

public class E<T> : D
{
public new T Foo
{
get
{
return (T)base.Foo;
}

set
{
base.Foo = value;
}
}
}
```

My suggestion is to add 2 new matchers:
```c#
public class MustMatchByNameAndTypeRule : IMatchingRule
{
public PropertyInfo Match(PropertyInfo subjectProperty, object expectation, string propertyPath)
{
PropertyInfo compareeProperty = expectation.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.SingleOrDefault(pi => pi.Name == subjectProperty.Name && pi.PropertyType == subjectProperty.PropertyType);

if (compareeProperty == null)
{
string path = (propertyPath.Length > 0) ? propertyPath + "." : "property ";

Execute.Verification.FailWith(
"Subject has " + path + subjectProperty.Name + " that the other object does not have.");
}

return compareeProperty;
}

public override string ToString()
{
return "Match property by name and type (or throw)";
}
}

public class TryMatchByNameAndTypeRule : IMatchingRule
{
public PropertyInfo Match(PropertyInfo subjectProperty, object expectation, string propertyPath)
{
PropertyInfo compareeProperty = expectation.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.SingleOrDefault(pi => pi.Name == subjectProperty.Name && pi.PropertyType == subjectProperty.PropertyType);

return compareeProperty;
}

public override string ToString()
{
return "Try to match property by name and type";
}
}
```
Comments: Fixed in commit https://fluentassertions.codeplex.com/SourceControl/changeset/3c0f594993f6c69e9e2c5c09d213edb44c771648

Edited Issue: False positive with Should().BeApproximatively and float.NaN value [12479]

$
0
0
Hi,

The method BeApproximatively does not fail if the actual value is float.NaN.
Here is the test :
```
[Fact]
public void Test_ShouldFail()
{
var actual = float.NaN;
var expected = 5f;
actual.Should().BeApproximately(expected, 0.01f);
}
```

Note that the test fails as expected with a double.

Edited Issue: False positive with Should().BeApproximatively and float.NaN value [12479]

$
0
0
Hi,

The method BeApproximatively does not fail if the actual value is float.NaN.
Here is the test :
```
[Fact]
public void Test_ShouldFail()
{
var actual = float.NaN;
var expected = 5f;
actual.Should().BeApproximately(expected, 0.01f);
}
```

Note that the test fails as expected with a double.

Commented Issue: False positive with Should().BeApproximatively and float.NaN value [12479]

$
0
0
Hi,

The method BeApproximatively does not fail if the actual value is float.NaN.
Here is the test :
```
[Fact]
public void Test_ShouldFail()
{
var actual = float.NaN;
var expected = 5f;
actual.Should().BeApproximately(expected, 0.01f);
}
```

Note that the test fails as expected with a double.
Comments: Fixed in commit https://fluentassertions.codeplex.com/SourceControl/changeset/98915d94b473cfb9215080e1ae0fd7bc6b0bb65e

Edited Issue: ShouldNotThrow hides stack trace of thrown exception [12473]

$
0
0
When an assertion of the `ShouldNotThrow` family of extension methods fails, the specifics of where in the SUT the exception was thrown is lost. This makes it hard to diagnose because you either need to attach a debugger, or remove the assertion to get the information needed to fix the issue.

Consider the following code (assume it is in a TestFixture):

[Test]
public void This_Should_Not_Throw()
{
Action act = ThrowException;
act.ShouldNotThrow();
}

private void ThrowException()
{
throw new Exception();
}

The failure message is:

>> Did not expect any exception, but found a System.Exception with message "Exception of type 'System.Exception' was thrown.".

and the stack trace shows only `This_Should_Not_Throw` followed by Fluent Assertion code.

If I replace the Fluent Assertion with NUnit's Assert.DoesNotThrow(new TestDelegate(act)), the stack trace is equally unhelpful; however I get this message:

>> Expected: No Exception to be thrown
But was: (Exception of type 'System.Exception' was thrown.)
at ExceptionTest.Class1.ThrowException() in c:\Users\adamv\Documents\Visual Studio 11\Projects\ExceptionTest\ExceptionTest\Class1.cs:line 21
at NUnit.Framework.Constraints.ThrowsNothingConstraint.Matches(Object actual)

If I simply take out the assertion and execute the delegate, I get a complete stack trace.

I would expect Fluent Assertions to provide the stack trace as part of the message in a way similar NUnit. Without this functionality it is better to use the NUnit assertion or use no assertion at all.

Commented Issue: ShouldNotThrow hides stack trace of thrown exception [12473]

$
0
0
When an assertion of the `ShouldNotThrow` family of extension methods fails, the specifics of where in the SUT the exception was thrown is lost. This makes it hard to diagnose because you either need to attach a debugger, or remove the assertion to get the information needed to fix the issue.

Consider the following code (assume it is in a TestFixture):

[Test]
public void This_Should_Not_Throw()
{
Action act = ThrowException;
act.ShouldNotThrow();
}

private void ThrowException()
{
throw new Exception();
}

The failure message is:

>> Did not expect any exception, but found a System.Exception with message "Exception of type 'System.Exception' was thrown.".

and the stack trace shows only `This_Should_Not_Throw` followed by Fluent Assertion code.

If I replace the Fluent Assertion with NUnit's Assert.DoesNotThrow(new TestDelegate(act)), the stack trace is equally unhelpful; however I get this message:

>> Expected: No Exception to be thrown
But was: (Exception of type 'System.Exception' was thrown.)
at ExceptionTest.Class1.ThrowException() in c:\Users\adamv\Documents\Visual Studio 11\Projects\ExceptionTest\ExceptionTest\Class1.cs:line 21
at NUnit.Framework.Constraints.ThrowsNothingConstraint.Matches(Object actual)

If I simply take out the assertion and execute the delegate, I get a complete stack trace.

I would expect Fluent Assertions to provide the stack trace as part of the message in a way similar NUnit. Without this functionality it is better to use the NUnit assertion or use no assertion at all.
Comments: Was already solved while adding support for AggregateExceptions.

Commented Unassigned: Collection should contain other collection behaving unexpectedly [12495]

$
0
0
I want to assert that a collection of simple objects includes a smaller collection of the same object type. For some reason actualCollection.Should().Contain(expectedCollection) is failing with a message that shows all of the expected objects are in the actual collection but it's claiming that they aren't.

Here's the assertion failure output I'm getting:
```
Expected collection {
ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "custom"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "friends"
percentage = 0
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "friends of friends"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "only me"
percentage = 0
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "public"
percentage = 0.333333333333333
}} to contain {

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "custom"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "friends of friends"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "public"
percentage = 0.333333333333333
}}, but could not find {

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "custom"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "friends of friends"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "public"
percentage = 0.333333333333333
}}
```
Comments: I only do component and integration level testing so I have literally no use for reference equality but I guess there might be reason to use it for unit tests. Can we do a poll on CodePlex regarding this? I have used the method described in your blog post before but it doesn't replace IEnumberable.Contains(), only IEnumberable.Equals(). I'm stuck with implementing Equals() for all of my objects using the current release.

Commented Unassigned: Collection should contain other collection behaving unexpectedly [12495]

$
0
0
I want to assert that a collection of simple objects includes a smaller collection of the same object type. For some reason actualCollection.Should().Contain(expectedCollection) is failing with a message that shows all of the expected objects are in the actual collection but it's claiming that they aren't.

Here's the assertion failure output I'm getting:
```
Expected collection {
ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "custom"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "friends"
percentage = 0
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "friends of friends"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "only me"
percentage = 0
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "public"
percentage = 0.333333333333333
}} to contain {

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "custom"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "friends of friends"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "public"
percentage = 0.333333333333333
}}, but could not find {

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "custom"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "friends of friends"
percentage = 0.333333333333333
},

ActivityServiceTest.DataTypes.SocialItemSummary
{
category = "public"
percentage = 0.333333333333333
}}
```
Comments: I do understand why you need structural equality in this particular case, but I'm in no way saying your should implement Equals() all over your code base. Only types that have value semantics need that. For instance, in Domain Driven Design, an object like you showed in your example would probably be a Value Object and immutable. In that case, it's quite natural to implement Equals(). Regardless, I'll consider your proposal. I just need to think of a good API.

Edited Issue: BeInOrder does not cater for duplicate values [12468]

$
0
0
The internal method BeInOrder does not cater for duplicate values.

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.

Commented Issue: BeInOrder does not cater for duplicate values [12468]

$
0
0
The internal method BeInOrder does not cater for duplicate values.

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.
Comments: Fixed in commit https://fluentassertions.codeplex.com/SourceControl/changeset/23647ff8786a7cabfd0974d3dcefb6b123722f41. The assertion itself was correctly implemented, but the index reported was wrong.

Edited Issue: Access to EventRecorder [12465]

$
0
0
Hi,

Would you consider making to possible to get the EventRecorder for an object and event, so that it is possible to write custom assertions. Without having to save the IEnumerable<EventRecorder> when calling MonitorEvents?

Best regards

Kim

Edited Feature: Access to EventRecorder [12465]

$
0
0
Hi,

Would you consider making to possible to get the EventRecorder for an object and event, so that it is possible to write custom assertions. Without having to save the IEnumerable<EventRecorder> when calling MonitorEvents?

Best regards

Kim

Commented Feature: Access to EventRecorder [12465]

$
0
0
Hi,

Would you consider making to possible to get the EventRecorder for an object and event, so that it is possible to write custom assertions. Without having to save the IEnumerable<EventRecorder> when calling MonitorEvents?

Best regards

Kim
Comments: Exposed the recorded through a dedicated extension method Exposed an extension method for obtaining an EventRecorder for a monitored object.

Edited Issue: Fluent assertions v1.7.1 fails to compare nullable timespans [12462]

$
0
0
Not tested with latest version. It seems that an overload on SimpleTimeSpanAssertions which accepts a Nullable<TimeSpan> is missing. I was able to work around this by writing the next extension method:

public static class SimpleTimeSpanAssertionsExtensions
{
// The current version of Fluent Assertions (v1.7.1) seems unable to compare nullable timespans.
// Example: myObject.OptionalTime.Should().Be(expectedOptionalTime);
// This method implements a workaround for this problem.
public static void Be(this SimpleTimeSpanAssertions source, TimeSpan? expected)
{
if (expected == null)
{
source.Subject.As<object>().Should().BeNull();
}
else
{
source.Subject.Should().Be(expected.Value);
}
}
}

I am aware that the return type should not be void and that this method does not have parameters for describing the error. But for the moment it fixes my problem at hand.

Best regards,
Bart Koelman
Viewing all 1402 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>