by Keyvan Nayyeri via Keyvan Nayyeri on 1/2/2008 8:23:56 PM
[Update: Jason Stangroome and Rich Grenwick left helpful comments. Rich has a good description of the problem, its reason and a fix for that. I applied his recommended code to BlogML test method and it passed successfully.]
Some readers may remember my post about the performance comparison between two approaches that I used to unit test BlogML API especially around the list of properties for two objects that have many properties.
Recently I got back to my unit testing progress and tried to improve my code coverage. I suddenly noticed a weird behavior in one of the test methods that I've written on top of the reflection technique. Actually I can't find any reason for this unexpected behavior and for now I can consider it as a problem for the runtime engine unless someone can give me some hints about the reason why this happens.
Before talking about the problem, let me introduce the situation. Below is an overridden Equals method for PostCollection class in BlogML library for .NET.
public override bool Equals(object obj)
{
PostCollection posts = obj as PostCollection;
int i = 0;
foreach (BlogMLPost post in posts)
BlogMLPost original = this[i];
foreach (PropertyInfo property in
Type.GetType(typeof(BlogMLPost).ToString()).GetProperties())
if (property.GetValue(post, null) != property.GetValue(original, null))
return false;
}
i++;
return true;
I also have a test method that simply tests two PostCollection objects two see if this method can check their equivalently.
[TestMethod()]
public void EqualsTest()
BlogMLBlog.PostCollection target = new BlogMLBlog.PostCollection();
// post1
BlogMLPost post1 = new BlogMLPost();
post1.Approved = true;
target.Add(post1);
// post2
BlogMLPost post2 = new BlogMLPost();
post2.Approved = true;
target.Add(post2);
object obj = target as BlogMLBlog.PostCollection;
bool expected = true;
bool actual;
actual = target.Equals(obj);
Assert.AreEqual(expected, actual,
"BlogML.Xml.BlogMLBlog.PostCollection.Equals did not return the expected value.");
Of course BlogMLPost has many more properties but this single Approved property would be enough for the test.
When I ran this test I expected to pass it easily but surprisingly I saw that it failed because the actual value was false while I expected true! Going back and forth, I ended up with a breakpoint in the code as you see below. I just wanted to see when my code steps inside the if case and returns false value.
Running the test method in debug mode I finally reached to the breakpoint. At this point I checked values for two sides of the if case expression to see which value they contain. The left hand side expression was evaluated as false.
The right hand side expression was evaluated as false, too.
Oddly enough, I stopped for a moment because I couldn't find any reason why this has happened so just monitored the value of the whole expression in my if case.
Very odd, (False != False) was evaluated as true and my method was returning the false value as the result. I have no idea why this is happening. Normally I'd expect this expression to be evaluated as false because this is what I learned in school for logic!
As an additional note, this occurs for the HasExcerpt property of the BlogMLPost class which is a Boolean property. I used same technique to unit test other collection types in BlogML library and didn't have any problem with them.
Do you have any idea why this is happening? If you want to test the code, just download the latest revision from the repository and run it yourself.
Original Post: When (False != False) is Evaluated As True
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.