|
May I suggest you read the discussion here: [^], and think about whether unti-testing of the private "details" of your objects is really a requirement.
But, if you have to do it, note that Microsoft's testing facility hands you tools like 'PrivateObject and 'PrivateType.
Without seeing your code where you try and test an 'Enum, I don't think much can be said except that, yes, it can be done.
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”
|
|
|
|
|
Thanks for the info.
Yes i wanted to check the private variable in unit testing as the method is having void as return type. So decided to check the variable values in order to do assertions.
I am able to get the private primitive data type [int, string] variables. but facing issue in accessing private custom data type i.e., user defined data types.
So please help me out.
|
|
|
|
|
/*I retrieve key value pairs for each of the conditions. The error is in the bolded. First condition returns longitude & latitude pairs. The third condition returns "State" and "WY" or "County"and "Buffalo", so that works fine. If it contains an "ID", it errors when the kvp consist of "value" = LocationID (corresponds to column name) that has empty string in uppercase "Value", because some are empty in the database */
public static tbl_Location ParseLocation(IOrderedDictionary values)
{
tbl_Location locFromValues = new tbl_Location();
foreach (DictionaryEntry value in values)
{
if (value.Key.Equals("Latitude") ||
value.Key.Equals("Longitude"))
{
double? valueAsNumber = null;
try { valueAsNumber = double.Parse(value.Value.ToString()); }
catch { }
locFromValues.GetType().GetProperty(value.Key.ToString()).SetValue(locFromValues, valueAsNumber);
}
else if (value.Key.ToString().Contains("ID"))
{
int? valueAsNumber = null;
try { valueAsNumber = int.Parse(value.Value.ToString()); }
catch { }
locFromValues.GetType().GetProperty(value.Key.ToString()).SetValue(locFromValues, valueAsNumber);
}
else
{
locFromValues.GetType().GetProperty(value.Key.ToString()).SetValue(locFromValues, value.Value);
}
}
return locFromValues;<pre lang="c#">
//I tried several ways for nullable int to take empty strings but it doesn't work, without adding another return in the catch. I have a return at the end of code block. Thank you.
|
|
|
|
|
Sam 9100 wrote: try { valueAsNumber = int.Parse(value.Value.ToString()); }
catch { }
That's a cardinal sin; an empty catch-block hides any error and acts as if nothing happened.
There's Int32.TryParse[^], which you might want to try;
if (!Int32.TryParse(Convert.ToString(value.Value), out valueAsNumber)
valueAsNumber = 0;
You could explicitly check for DBNull.Value and assign a value for empty-fields only, but the above would assume that everything that can't be converted to a number is equal to zero.
Do take into consideration that the example is non-localized and simplified; you might need to specify which culture you are expecting to validate the string correctly as a number.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thank you for the reply Eddie. That code did not work either. I wish I could post screen shot for you. I inherited this project so here is more info to help.
This BLL code loops thru COLs and values with same values in the db. This particular field is datatype int that allows null. Due to key value pairs, the .Contains("ID") will show key=StatusID value=2004 (which works fine), or key=PostID value="". If empty strings, it shows null for int? Hence the "input string incorrect format" error.
int? valueAsNumber = null; shows null
During debug, the begin code and next step after this section wants it as an int. Here due to the key pair values, it does not like empty strings. But it allows null in db? It also says "to ignore exceptions to retain NULL when parsing" only in this section are the Catch blocks empty.
This is MVC, Entity Framework 6.1, C#, asp.net 4.5
I also tried if the value contains("ID") or Contains("") or == null, it didnt like it. The textbox expects an int returned
|
|
|
|
|
Eddie's right about the code smell of the try with an empty catch . Use int.TryParse to determine if there's a parsing error.
If value.Value is an empty string then the parsing will fail.
(Since you aren't getting a NullReferenceException on the .ToString() it means that value.Value isn't null .)
So what you should do first is to check value.Value for being empty.
public static tbl_Location ParseLocation(IOrderedDictionary values)
{
tbl_Location locFromValues = new tbl_Location();
Type locType = typeof(tbl_Location);
foreach (DictionaryEntry value in values)
{
string key = value.Key as string;
if (key == null)
throw new InvalidOperationException(string.Format("Key is not a string: {0}", value.Key);
string val = value.Value as string;
bool emptyValue = string.IsNullOrEmpty(val);
object storableValue = null;
if (key.Equals("Latitude") || key.Equals("Longitude"))
{
double parsed;
if (!emptyValue && double.TryParse(val, out parsed))
storableValue = parsed;
}
else if (key.Contains("ID"))
{
int parsed;
if (!emptyValue && int.TryParse(val, out parsed))
storableValue = parsed;
}
else
{
storableValue = val;
}
locType.GetProperty(key).SetValue(locFromValues, storableValue);
}
return locFromValues;
}
All of a sudden you mention TextBox .
The TextBox's .Text property expects a string value.
So where is the value from the PostID property of the tbl_Location get put into this TextBox 's .Text property you mention?
That is probably where the "null" from "parsing" the empty string gets displayed.
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
Sorry for the late reply, but thank you both for your help. There is more to it than this from what I investigate. However, I have replaced the empty catch block with the if else TryParse. It is cleaner and better. What this is supposed to do is retrieve matching data from gridviewOne and gridviewTwo, and displaying results in gridviewResults with a new ID. The db is returning the ID to the BLL class (above), but it does not return all the way to the view forms. So the actual error has been resolved, but a prior change (undocumented) occurred where an ID was overridden so the next error I generate is object null reference error. My CO brought me in to fix the bug because no one knows what happened. But I appreciate your assistance and I will gladly post for more help as you guys are the best! Keep up the good work!
|
|
|
|
|
I am getting error in the bold line. i have column name as paword but its showing column name doesnt belong to the table
public Userdetails GetUserDetailsByEmailID(String EmailID)
{
Userdetails userInfobyEmailID = null;
NpgsqlParameter[] parameters = new NpgsqlParameter[] {
new NpgsqlParameter("@email", EmailID)
};
using (DataTable table = Helper.ExecuteParamerizedSelectCommand("forgetpassword", CommandType.StoredProcedure, parameters))
{
if (table.Rows.Count > 0)
{
userInfobyEmailID = new Userdetails();
foreach (DataRow row in table.Rows)
{
userInfobyEmailID.paword = Utilities.Decrypt(row["paword"].ToString());
}
}
return userInfobyEmailID;
}
}
|
|
|
|
|
Are you certain that is the correct spelling of the column name?
|
|
|
|
|
no in table also name is same
|
|
|
|
|
I find that hard to believe. Show the exact details of what gets posted into your DataTable from the stored procedure.
|
|
|
|
|
Also, please do not post the same question in a forum and QA.
|
|
|
|
|
Did you miss all those replies to your last question where we suggested you read up on how to securely handle passwords in your application? Or did you just choose to ignore them?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
The passwords are well hidden in a field with unknown name...
|
|
|
|
|
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Dear All,
I want to buy Data Access Layer component for my project.
Anyone could give me advise.
Thanks,
LVD
|
|
|
|
|
levanduyet_vn wrote: I want to buy Sorry, this is not an online shopping site.
|
|
|
|
|
Hi Richard,
I just want to get help from expert and get their advise.
|
|
|
|
|
levanduyet_vn wrote: I just want to get help Then you need to explain what your problem is.
|
|
|
|
|
levanduyet_vn wrote: I want to buy Data Access Layer component for my project. Why? There are many free ones available and it's incredibly trivial to write your own DAL.
This space for rent
|
|
|
|
|
Dear Pete,
Please give me the links that I could refer.
Thanks,
LVD
|
|
|
|
|
In the time you took to write that, you could have used Google[^] to find examples. There's a CP one there that shows a simple DAL.
This space for rent
|
|
|
|
|
Hi Pete,
Sure, I have google but I need to have a advise from who have used it.
LVD
|
|
|
|
|
maybe this one[^] works for you?
It is very simple and quick to use. It is also very stable.
It might be limited in possibilities, but using the code you can extend as much as you want.
Hope this helps.
|
|
|
|
|