This is a migrated thread and some comments may be shown as answers.

Back to basics - Assert.AreSame and Assert.Equals

3 Answers 75 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 13 Jun 2014, 11:18 AM
Hi Guys

Hopefully someone can clear something up for me.

I am trying to to a simple Assertion to confirm that two strings are the same.

I have a field on a form, I edit the form > enter a value > extract the text as a variable > save the changes
Then on the main form the updated value should be displayed > I used dev tools to get the ID of the field as its a Kentico control > then in code I want to assert that the two values are the same. if they are the same then the test is valid otherwise it should fail.

From my understanding, Assert.AreSame checks if the two objects are the same object and Assert.Equals just checks if the strings or whatever are the same.  Am I right?

See the code below:

Assert.AreSame(town, Find.ById("ctl00_ctl00_cphMain_cphMain_TownLabel").TextContent);

or

Assert.Equals(town, Find.ById("ctl00_ctl00_cphMain_cphMain_TownLabel").TextContent);

Does anyone have any ideas why my Equals assertion passes if it has two different values and if they are the same.

Any help would be much appreciated


Thanks

Michelle

3 Answers, 1 is accepted

Sort by
0
Miguel
Top achievements
Rank 1
answered on 13 Jun 2014, 04:16 PM
Hi Stephen,
I would suggest using the String.Equals() inside an Assert.IsTrue() as you are comparing two strings and Assert.AreSame and Assert.Equals might be comparing their instances which will not be the same.

Assuming town is a String variable the following should work:
Assert.IsTrue(town.Equals(Find.ById("ctl00_ctl00_cphMain_cphMain_TownLabel").TextContent));

A good way to ensure that you are in fact obtaining the values you are expecting is to write them to the log:
Log.WriteLine(town + "----" + Find.ById("ctl00_ctl00_cphMain_cphMain_TownLabel").TextContent);
0
Shashi
Top achievements
Rank 1
answered on 13 Jun 2014, 05:04 PM
Assert.AreEqual should work if you want to compare the values of two strings (it also works for ints, floats, doubles and a host of other object types).  We use it extensively in our code without issues.  I prefer it to Assert.IsTrue(String.Equals ...) because
   a) It makes the code simpler and more understandable and 
   b) (more important) It has a built-in failure error message that provides both expected and actual values (so I don't have to design a custom error message to provide the same info - which I have to do for Assert.IsTrue). 

If it is not working (passing for two values that are not the same), there may be something wrong with the values being passed to it.  I would use Log.WriteLine as Miguel suggests - or if you are working inside Visual Studio, run the test in Debug, break just before it executes the assert and examine the values of each parameter. 

Also, verify the following:
a) Objects being passed in to the Assert are both of type string (assign them to string objects for debugging purposes - you can remove these later if you want).
b) Find.ById is returning the object you are expecting (for debugging purposes you may want to split the Find.ById statement into separate statements - one to call Find.ById and assign it to an object of the type you are expecting and the other to get the  textcontent property from that object).

One other gotcha with Assert.AreEqual:  Make sure you are passing values in the correct order - otherwise your error message will be misleading.  Expected value should be the first parameter (which is somewhat unintuitive - at least to me).

Hope that helps,

Shashi
0
Stephen
Top achievements
Rank 1
answered on 16 Jun 2014, 08:49 AM
Miguel and Shashi,

Thank you both for your detailed explainations, they are much appreciated.  I'll give these approaches a go and I'm sure they will sort out the issue.

Regards
Tags
General Discussions
Asked by
Stephen
Top achievements
Rank 1
Answers by
Miguel
Top achievements
Rank 1
Shashi
Top achievements
Rank 1
Stephen
Top achievements
Rank 1
Share this question
or