|
Luc Pattyn wrote: change all private attributes to protected ones
that was exagerated, it should read: change private attributes to protected ones as required...
Yes, making everything protected (or public) is not highly regarded as it opens the door more than may be required by the app itself.
I listed some possibilities and already said they all have some drawback; do you have a better solution?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
I would never change a private method to public or anything else for testing. I'd always test it through calls to the public[1] members of the class because that's the way that they were designed to be used. If you make them public and test them directly then you have access to them in ways that the other classes and modules don't and you can stress them in ways that won't be possible once deployed. When unit testing a class I access it only in ways it was designed to be used and if code private that means that it was not designed to be used by anything other than the other code in the class.
Take the following class:
public class TestClass
{
private void PrivateMethod(bool someBool)
{
if (someBool)
{
Console.WriteLine("Reachable Code");
}
else
{
Console.WriteLine("Dead Code");
}
}
public void PublicMethod(double x)
{
double calculationResult = Math.Abs(x);
PrivateMethod (calculationResult >= 0.0);
}
}
The private method has different behavior based on its parameters. If you make that public and test it directly then you can stress both cases but that's not how the code was designed! If you examine the code you'll see that the only place it's called from is from the one public method and that method will always pass it a value of true . What this means is that private method doesn't need to have different behaviors, it doesn't need to check its input and it doesn't even need any input parameters. The parameter, if statement and else block are all dead code and should be removed. At least this is what it looks like at first glance; what if the fact that the method can only be passed one value a bug? What if the calculation is wrong? If you make the private method public just for the sake of testing then this question might never be asked. Granted, this is a contrived example that shows a minor inefficiency but it highlights how easy it is to hide bugs private code if you tweak for testing.
If you have a class where everything is private then what's the point of the class? If you have private code that isn't callable in one way or another from any public methods then what's the point of the code? If code can't be executed then it shouldn't exist, otherwise it should only be accessed in the manner in which it was designed to be accessed whether it's in testing or deployment.
In the test class above I would test the private method by writing test cases that enter whatever public methods are required to fully exercise as much of the private method as possible.
Of course, that's just my methodology. YMMV.
[1] "public" in this post is used as shorthand for any accessibility level that allows the method to be called from another class regardless of whether that class is internal to the assembly, an inherited child or using public attributes.
[EDIT] corrected example code
modified on Monday, February 15, 2010 7:06 AM
|
|
|
|
|
Some interesting comments here.
I don't completely agree with what you are saying about only testing public members. Firstly a unit test is supposed to test a unit eg. a single method, and if you don't call private members directly then it's not really unit testing.
I also like to test things thouroughly which may include using things in a way that they weren't necessarily supposed to be used. Dead code is potentially a bug waiting to happen IMHO. Imagine the code is changed in the future and the dead code comes to life. People will think the dead code has been tested and will trust is, but in reality it has probably never been executed and may have bugs in it. Statistically there is a higher chance of finding bugs around redundant code so I would want to know about the dead code. If it wasn't needed I would delete it or at least comment it out.
I agree that it's not a good idea to change the accessiblity for testing, and I don't plan to do this. However, in the real world it may be worth making a few modifications for testing. In the past I have made a change that allowed me to change the database connection string for testing, and in my opinion, it didn't really reduce the quality of the code.
|
|
|
|
|
Member 4487083 wrote: only testing public members
I'm not saying don't test them, I'm saying don't call them directly to test them. To test the private method in my example I'd write test cases that started with the public method and manipulate the inputs in order to fully exercise the private one.
Member 4487083 wrote: a unit eg. a single method
I'd call a unit a class, not a method or a property. When I link to a third party DLL to import some functionality, I'm importing the classes not individual methods.
Member 4487083 wrote: which may include using things in a way that they weren't necessarily supposed to be used
Of course, but I always honor the contract of the classes that I'm testing. If a class has a method that takes a string parameter I'll throw everything I can think of into that parameter to stress the method but if you go outside the contract then what are you testing? If something is private then it's not supposed to be accessible by anything other than things internal to the class so it should be tested only by way of things internal to the class.
Member 4487083 wrote: Dead code is potentially a bug waiting to happen IMHO
I agree completely.
Member 4487083 wrote: it may be worth making a few modifications for testing
I agree almost completely. Testability is something that can/should be built in so that no modifications are required to do it. The connection string is a good example. Ideally those should be built to be configurable from the beginning - it shouldn't have to be added on later just to accommodate testing. Still, this is something completely different from changing the accessibility level of members to facilitate testing. Making something configurable is an enhancement that promotes flexibility and testability; making something unnecessarily public makes it more testable but introduces security risks. More to the point, making a connecting string configurable doesn't change the contract of the class but changing something from private to public does.
|
|
|
|
|
Hi,
I've an application which crashes inside InitializeComponent() method. It works on most of the computers but crashes on certain computers. If you need I can post the code of my InitializeComponent() method.
Any ideas?
Thanks.
|
|
|
|
|
SimpleData wrote: you need I can post the code
Please do.
No ideas as yet.
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
It is just designer generated code, nothing special. But there you go.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(login));
this.label1 = new System.Windows.Forms.Label();
this.btnLogin = new System.Windows.Forms.Button();
this.txtUsername = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPassword = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.cbTVmode = new System.Windows.Forms.CheckBox();
this.pbPartnerLogo = new System.Windows.Forms.PictureBox();
this.label6 = new System.Windows.Forms.Label();
this.cbWebSite = new System.Windows.Forms.ComboBox();
this.chAutoLogin = new System.Windows.Forms.CheckBox();
this.chRememberMe = new System.Windows.Forms.CheckBox();
this.panel2 = new System.Windows.Forms.Panel();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pbPartnerLogo)).BeginInit();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
|
|
|
|
|
How do you load images into the Picture boxes that you are using?
For a correct approach, have a look here.Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
Thank you for your answer, but according to the link you've given I am doing it correctly.
|
|
|
|
|
Hi,
every WinForm app should have a try-catch around its Application.Run() statement; it would provide a safety net for all mishaps that could occur in the main form's construction. So try it like this:
public static void Main() {
try {
...
Application.Run(new Form1());
} catch(Exception exc) {
Console.WriteLine(exc.ToString());
}
}
That should give you the stack trace and point you straight at the problem.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
modified on Sunday, February 14, 2010 7:03 AM
|
|
|
|
|
Thanks, I will try it and let you know.
|
|
|
|
|
Hello every body,
I've developed a .NET C# application. I have a strang problem here, Some times while working in the application It stops suddenly with no error messages or any thing. I remember when I was in the debug mode , it did the same problem also and without error messages or exceptions.I cant recognize the time it stops (may be it is not fixed ?). The program do alot of things, it's mainly for scoreboards control but it uses .Net libraries for capturing videos from TV card, access a shared folder in the network and uses custome made controls (buttons ).
How can I know the reason of this problem ???All You Need Is A Good Friend
|
|
|
|
|
Goto to the Debug menu, open the Exceptions box and select all options in the "Break when an exception is" dialog.
Maybe that will allow you to see an error message in the application.Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
How can I convert the IPictureObject of MODI.Image.Picture to a "standard"
image (System.Drawing.Image or System.Drawing.Bitmap) in C#
thanks to allHAVE A GOOD TIME
|
|
|
|
|
Although I have never used IPictureObject , I can suggest if you can get the handle of the image, you can use Image.FromHBitmap method to convert it to an image object.
Hope this helps."No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem
|
|
|
|
|
thanks.i change it , but not work HAVE A GOOD TIME
|
|
|
|
|
If you can post the code how you did that, may be someone familiar with the interface you are dealing with can help. "No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem
|
|
|
|
|
thanks d@nish for your reply.sample program is paperless desktop in c# forum.link is here The Paperless Desktop[^]
i ask this question from martin but , i dont receive answer yetHAVE A GOOD TIME
|
|
|
|
|
Hi,
can any one please do et me know if we could insert an image into an xml if so please do let me know how.
my task is to read an image from .xml file and save it in to databae
|
|
|
|
|
Here is way to write images to an XML file:
Image image = Image.FromFile(@"image file path");
MemoryStream memStream = new MemoryStream();
image.Save(memStream, image.RawFormat);
byte[] array = memStream.ToArray();
string imageString = Convert.ToBase64String(array);
Now all you have to do is to write this string to the XML file.
In order to read the image from the string, you can use this:
string imageString = "read the image string from XML file";
byte[] imageBytes = Convert.FromBase64String(imageString);
MemoryStream memStream = new MemoryStream(imageBytes);
Image image = Image.FromStream(memStream);
Although, it would be better if you store image files at a location and just the path in the XML files. In case you are dealing with large images, you would have a very bulky XML file."No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem
|
|
|
|
|
Hi Dinesh,
can i know how to insert an image in an xml file sice xml file consists of only tags can you please do send me the sample link if any to my mail so that i can make samples of it
|
|
|
|
|
He showedspoonfed you how to turn the image into an "xml-clean" string, surely you know how to insert a string into an xml file, otherwise how can you be using an xml file at all?
|
|
|
|
|
I already posted the code. What else are you looking for? "No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem
|
|
|
|
|
I'm creating a series of controls that I need to draw myself.
They are all derived from Control and each overrides OnPaint().
The main parent control paints itself fine. It maintains a list of the second-level controls and they paint fine too. Each of these second-level controls contain their own lists of third-level controls, that ideally will draw themselves too but at the moment, OnPaint(), does not get called for them.
Seems strange that the other two classes are able to paint and this last one is not.
|
|
|
|
|
Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks
public void restartserver(string serverrs, string serverloc)
{
Process p = Process.GetCurrentProcess();
Process[] servername = Process.GetProcessesByName(serverrs);
IntPtr serverHandle = FindWindow(null, serverloc);
if (serverHandle == IntPtr.Zero)
{
if (serverrs == "arcemu-world")
{
btnWorldStatus.Text = "World Server is not running";
}
else
{
btnLogonStatus.Text = "Logon Server is not running";
}
}
else
{
SetForegroundWindow(serverHandle);
SendKeys.SendWait("Shutdown{ENTER}");
FreeConsole();
bool mytrue = true;
while (mytrue)
{
if (p.HasExited)
{
mytrue = false;
if (serverrs == "arcemu-world")
{
btnWorldStatus.Text = "Closing....";
}
else
{
btnLogonStatus.Text = "Closing....";
}
}
}
}
}
|
|
|
|