You can either pass a 'because' reason to the assertion method and use that to clarify the assertion failure. Or, you could temporarily set the Verification.SubjectName. If any value exists, it will be used to make the message more specific.
New Post: I want to test for property value by property name
New Post: I want to test for property value by property name
Sorry,
Let me clarify. I want to make a statement like:
actual.ShouldHave().Properties(propertyName).EqualTo(new Dictionary<string,object> {propertyName, value});
New Post: I want to test for property value by property name
But that's how it already works. Whenever a property doesn't match, it will show you which property mismatches.
New Post: I want to test for property value by property name
Yes but I have to specify a property expression not a property name.
I'd like to know if there is a way to do this using the property name.
I'm sorry, I apparently phrased this question terribly. :)
New Post: I want to test for property value by property name
Ah, you should use the new API that replaces the ShouldHave() implementation. You can read all about it here.
New Post: Getting all the differences between two objects
Hi,
Using fluent assertions 2.0 , can I get all the differences between two objects of the same class, and not just whether they are equal?
Also, is there a method for comparing two list properties regardless of the items order in the list? (in fluent assertions 1.7 we developed a method just for this sort of comparing).
Thanks!
New Post: Getting all the differences between two objects
Hi,
Thanks for taking the time to post your question. Unfortunately, the current implementation for verifying equivalency only reports the first difference it finds. It might be possible to collect all differences, but that requires a change in the implementation.
About the list properties, you can should collection.Should().BeEquivalentTo(collection). This will ensure both collections contain the same items, but doesn't care about the order. It was part of 1.7 as well though.
Regards,
Dennis
New Post: Getting all the differences between two objects
Thanks for the quick answer! :)
Created Issue: silverlight 5 test projects throwing wrong exceptions [12474]
I tried recompiling to SL5, and there's an errror in the GenerateHandler method of the EventHandlerFactory. the DynamicMethod constructor that is being used is not supported in SL5. I removed the module from this, and all the tests are running correctly.
The problem is that I am not sure if I broke something, as I don;t understand the code I touched.
Created Issue: PropertyAssertion does not throw if a class overwrites Equals(object obj) [12475]
```
class ClassThatOverwritesEquals
{
public int Age { get; set; }
public override bool Equals(object obj) {
return true;
}
}
```
This will never throw if you compare two instances of the same type:
```
var subject = new ClassThatOverwritesEquals {
Age = 36,
};
var expectation = new ClassThatOverwritesEquals {
Age = 37,
};
Action act = () => subject
.ShouldHave()
.AllProperties()
.EqualTo(expectation, "because {0} are the same", "they");
```
I would guess the problem is in ReferenceEqualityEquivalencyStep.cs line 43:
```
return !ReferenceEquals(context.Subject, null) && context.Subject.Equals(context.Expectation);
```
The Equals(..) should be replaced by object.ReferenceEquals(..).
Commented Issue: PropertyAssertion does not throw if a class overwrites Equals(object obj) [12475]
```
class ClassThatOverwritesEquals
{
public int Age { get; set; }
public override bool Equals(object obj) {
return true;
}
}
```
This will never throw if you compare two instances of the same type:
```
var subject = new ClassThatOverwritesEquals {
Age = 36,
};
var expectation = new ClassThatOverwritesEquals {
Age = 37,
};
Action act = () => subject
.ShouldHave()
.AllProperties()
.EqualTo(expectation, "because {0} are the same", "they");
```
I would guess the problem is in ReferenceEqualityEquivalencyStep.cs line 43:
```
return !ReferenceEquals(context.Subject, null) && context.Subject.Equals(context.Expectation);
```
The Equals(..) should be replaced by object.ReferenceEquals(..).
Comments: Hmm, so I'm wondering now what would happen if you do that with primitive types. An integer of 2 in one type might not be 'reference equal' with another integer with value 2. Maybe it should only use Equals for primitive types. For non-primitive types, it should still use ReferenceEquals. Any ideas?
Commented Issue: silverlight 5 test projects throwing wrong exceptions [12474]
I tried recompiling to SL5, and there's an errror in the GenerateHandler method of the EventHandlerFactory. the DynamicMethod constructor that is being used is not supported in SL5. I removed the module from this, and all the tests are running correctly.
The problem is that I am not sure if I broke something, as I don;t understand the code I touched.
Comments: You're right. But it only happens when you actually compile the Silverlight version of FA in SL5. The code that you are referring to isn't even called at runtime, so normally it shouldn't be a problem at all. Are you sure the FileNotFoundException is caused by this specific issue?
Edited Feature: String comparison with 'ignore whitespace' [12423]
Commented Issue: PropertyAssertion does not throw if a class overwrites Equals(object obj) [12475]
```
class ClassThatOverwritesEquals
{
public int Age { get; set; }
public override bool Equals(object obj) {
return true;
}
}
```
This will never throw if you compare two instances of the same type:
```
var subject = new ClassThatOverwritesEquals {
Age = 36,
};
var expectation = new ClassThatOverwritesEquals {
Age = 37,
};
Action act = () => subject
.ShouldHave()
.AllProperties()
.EqualTo(expectation, "because {0} are the same", "they");
```
I would guess the problem is in ReferenceEqualityEquivalencyStep.cs line 43:
```
return !ReferenceEquals(context.Subject, null) && context.Subject.Equals(context.Expectation);
```
The Equals(..) should be replaced by object.ReferenceEquals(..).
Comments: Thanks for the fast response! The classes name is _ReferenceEqualityEquivalencyStep_ therefore I would expected it to be always __false__ for value types. Just try this: ``` int a = 2; bool result = object.ReferenceEquals(a,a). ``` The result will always be false. I don't know if ReferenceEqualityEquivalencyStep is used in other test scenarios (where .Equals() is required to get it work properly) :-). However, if you have a look at the uploaded PropertyAssertionSpecs2.cs you will notice that the test will fail with value types as well. A struct can overwrite Equals() too. Best Regards, Daniel
Commented Issue: PropertyAssertion does not throw if a class overwrites Equals(object obj) [12475]
```
class ClassThatOverwritesEquals
{
public int Age { get; set; }
public override bool Equals(object obj) {
return true;
}
}
```
This will never throw if you compare two instances of the same type:
```
var subject = new ClassThatOverwritesEquals {
Age = 36,
};
var expectation = new ClassThatOverwritesEquals {
Age = 37,
};
Action act = () => subject
.ShouldHave()
.AllProperties()
.EqualTo(expectation, "because {0} are the same", "they");
```
I would guess the problem is in ReferenceEqualityEquivalencyStep.cs line 43:
```
return !ReferenceEquals(context.Subject, null) && context.Subject.Equals(context.Expectation);
```
The Equals(..) should be replaced by object.ReferenceEquals(..).
Comments: To complete my last comment: I would say ReferenceEquality is not applicable on value types. Maybe using a different EquivalancyStep that takes advantage of ValueType.Equals(..) ?
Commented Feature: String comparison with 'ignore whitespace' [12423]
Comments: I'm trying to come up with a fluent API for that, but haven't come up with anything other than adding an extra parameter to the existing BeEquivalentTo(). Why don't you use string.Should().Match() instead?
Edited Feature: String comparison with 'ignore whitespace' [12423]
Edited Feature: 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?
Edited Feature: 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?
Created Issue: System.IO.FileLoadException in AttributeBasedFormatter.AllTypes [12476]
```
Test(s) failed. System.IO.FileLoadException : Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at FluentAssertions.Formatting.AttributeBasedFormatter.GetExportedTypes(Assembly assembly) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Formatting\AttributeBasedFormatter.cs:line 126
at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at FluentAssertions.Formatting.AttributeBasedFormatter.get_AllTypes() in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Formatting\AttributeBasedFormatter.cs:line 119
at FluentAssertions.Formatting.AttributeBasedFormatter.FindCustomFormatters() in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Formatting\AttributeBasedFormatter.cs:line 63
at FluentAssertions.Formatting.AttributeBasedFormatter.get_Formatters() in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Formatting\AttributeBasedFormatter.cs:line 58
at FluentAssertions.Formatting.AttributeBasedFormatter.GetFormatter(Object value) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Formatting\AttributeBasedFormatter.cs:line 0
at FluentAssertions.Formatting.AttributeBasedFormatter.CanHandle(Object value) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Formatting\AttributeBasedFormatter.cs:line 27
at FluentAssertions.Formatting.Formatter.<>c__DisplayClass1.<ToString>b__0(IValueFormatter f) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Formatting\Formatter.cs:line 54
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
at FluentAssertions.Formatting.Formatter.ToString(Object value, Boolean useLineBreaks, IList`1 processedObjects, Int32 nestedPropertyLevel) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Formatting\Formatter.cs:line 54
at FluentAssertions.Execution.Verification.<BuildExceptionMessage>b__3(Object a) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Execution\Verification.cs:line 217
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
at FluentAssertions.Execution.Verification.BuildExceptionMessage(String failureMessage, Object[] failureArgs) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Execution\Verification.cs:line 217
at FluentAssertions.Execution.Verification.FailWith(String failureMessage, Object[] failureArgs) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Execution\Verification.cs:line 158
at FluentAssertions.Numeric.NumericAssertions`1.Be(T expected, String reason, Object[] reasonArgs) in c:\Workspaces\codeplex\fluentassertions\Main\FluentAssertions.Net35\Numeric\NumericAssertions.cs:line 44
```
IMHO, it would be Ok to handle FileLoadException the same way other exceptions are handled in GetExportedTypes.
Thanks.