|
i'm doing vsts2008 unit testing. How m i assign date to following test method?
public void btnAddEntry_ClickTest()
{
frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value
object sender = null; // TODO: Initialize to an appropriate value
EventArgs e = null; // TODO: Initialize to an appropriate value
target.btnAddEntry_Click(sender, e);
//Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
|
|
|
|
|
as a parameter, no?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
yes.wan to know how to assign a value to test method.
|
|
|
|
|
perhaps try creating your own eventargs class that inherits from EventArgs then you can put any properties you want to use in it and pass that to the button click handler
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
|
shennwooi wrote: Can i assign some string or integer to EventArgs?
NO.
If there is a good reason for doing this, create your own class which derives from EventArgs . Use implicit operator. Eg:
class MyEventArgs : EventArgs
{
string name;
public string Name { get { return name; } }
public MyEventArgs(string name)
{
this.name = name;
}
public static implicit operator MyEventArgs(string str)
{
return new MyEventArgs(str);
}
}
class Program
{
static void Main(string[] args)
{
MyEventArgs ar = "hello";
Console.WriteLine(ar.Name);
}
}
|
|
|
|
|
use this
btnTest_Click(this, null);
to test button click.
*12Code
|
|
|
|
|
what if i wan to test the following function?
private void btnAddEntry_Click(object sender, EventArgs e)
{
double gas, miles;
int i;
string d;
char c;
bool validDate;
// Validate date format (actual values not checked)
// strip out any spaces
d = "";
for (i = 0; i < txtDate.Text.Length; i++)
{
if (txtDate.Text.Substring(i, 1) != " ")
{
d += txtDate.Text.Substring(i, 1);
}
}
txtDate.Text = d;
validDate = true;
// make sure date has eight characters (two are slashes remainder are numbers)
if (txtDate.Text.Length != 8)
{
validDate = false;
}
else
{
for (i = 0; i < 8; i++)
{
c = Convert.ToChar(txtDate.Text.Substring(i, 1));
if (i == 2 || i == 5)
{
if (c != '/')
{
validDate = false;
}
}
else
{
if (c < '0' || c > '9')
{
validDate = false;
}
}
}
}
if (!validDate)
{
MessageBox.Show("Date must be in form mm/dd/yy", "Date Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtDate.Focus();
return;
}
// Make sure miles is greater than previous value
miles = Convert.ToDouble(txtMiles.Text);
if (numValues > 0)
{
if (miles <= odometer[numValues - 1])
{
MessageBox.Show("Odometer reading less than previous value.", "Odometer Reading Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtMiles.Focus();
return;
}
}
// No zero gallons allowed
gas = Convert.ToDouble(txtGallons.Text);
if (gas <= 0)
{
MessageBox.Show("Gallons pumped must be greater than zero", "Gallons Pumped Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtGallons.Focus();
return;
}
dates[numValues] = txtDate.Text;
odometer[numValues] = miles;
gallons[numValues] = gas;
numValues++;
NewEntryLine();
txtDate.Text = GetToday();
txtMiles.Text = "";
txtGallons.Text = "";
txtDate.Focus();
}
|
|
|
|
|
Hi,
split your code into two methods:
1. one contains the "bunsiness logic" and no GUI stuff (No MessageBox); it does not take sender or eventargs as parameters, and it does return a calculated value and outputs (through out parameter) a string which should be shown to the user.
2. the buttonClickHandler which mainly calls the other method and shows the MessageBox when there is a need to.
private void btnAddEntry_Click(object sender, EventArgs e) {
string s;
double result=calculate(out s);
if (s==null) textBox.Text="The result is "+result;
else MessageBox.Show(s);
}
private double calculate(out string message) {
message=null;
double result=...;
if (...) message="Something went wrong";
...
return result;
}
That way you can easily perform unit tests on the business logic, and there probably won't be a need to test the actual buttonClickHandler anymore.
|
|
|
|
|
You've had some good answers. To add to them, you can't use your derived event args for existing events unless you create your own controls derived from the original.
For example, maybe you want to assign the integer value 1 to the event args passed when a form is loaded. You need to create your own EventArgs and your own Form with a new Load event and override the OnLoad method. Something like this.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : IntForm
{
public Form1()
{
InitializeComponent();
this.Load += new System.EventHandler<inteventargs>(Form1_Load);
}
private void Form1_Load(object sender, IntEventArgs e)
{
Console.WriteLine(e.Value);
}
}
public class IntEventArgs : EventArgs
{
private int _Value;
public IntEventArgs(int value)
{
_Value = value;
}
public IntEventArgs() : this(0) { }
public static implicit operator IntEventArgs(int value)
{
return new IntEventArgs(value);
}
public int Value
{
get { return _Value; }
}
}
public class IntForm : Form
{
public new event EventHandler<IntEventArgs> Load;
protected override void OnLoad(EventArgs e)
{
EventHandler<IntEventArgs> eh = Load;
if (eh != null)
eh(this, 1);
}
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
I'm pretty new in c# and i have a problem. I have an interrupt and i want to show some data on the form. to do that i need to delegate to obtain the form. I want to have the whole form because i have to put lots of information on the form. I've tried this:
<br />
public optionbox getOptionbox()<br />
{<br />
if (box.InvokeRequired)<br />
{<br />
optionboxThreadCallBack d = new optionboxThreadCallBack(getOptionbox);<br />
return d();<br />
} else<br />
return box; <br />
}<br />
but i get an stack overflow. The solution needs to work and doesn't needs to be the best. Anyone a suggestion?
|
|
|
|
|
Of course you do, it keeps recursing. What do you want to achieve ? How does your callback class help ?
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
On an interrupt it must show information on the form(called box) when i do this it gets a cross thread exception. I have seen the article about cross thread handling but because i need to put a lot of information on the form i was trying to get the whole form.
|
|
|
|
|
I'm not sure I would use C# for interrupt drivers, but anyway...
Joramq wrote: I have an interrupt and i want to show some data on the form.
Rule one of interrupts: KEEP THEM SHORT.
Do not try to update a form under interrupt, it won't work.
What you need to do is start a new thread to handle data from the interrupt and pass the data through some sort of buffer - normally a circular fifo is a good idea.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Joramq wrote: but i get an stack overflow.
Because you are calling the same function again and again. Read What's up with BeginInvoke?[^] for a better understanding.
|
|
|
|
|
ok i have read it, but now i only don't know how to return arguments when using a BeginInvoke where can i find that?
|
|
|
|
|
You don't return. You need to create a delegate which can take the value you need to pass as a parameter. When calling the delegate, just pass the value as parameter instead of returning.
|
|
|
|
|
oke, but i need to put a lot of information on the form, do i need to create a delegate for every textbox?
|
|
|
|
|
No just a parameter for every control, or a list, or a very long string with separators(not the best way), or a datatable, or...
Basicly your delegate will call a function and in that function you then have those parameters and can assign them to the right control (aka fill in the textbox)
|
|
|
|
|
Joramq wrote: do i need to create a delegate for every textbox?
No. Create a class which contains all required fields and pass the class instance through parameter.
|
|
|
|
|
Hi,
this won't work. Even when you fix the problems and get the optionbox returned, you can't touch it from another thread, so you still need a lot of InvokeRequired/Invoke stuff to feed it all the data.
Here is a better approach: put all the data you want to show or update on the form into one object (create a Results class), and teach your form to show a Results object. Then use the InvokeRequired/Invoke pattern to pass one Results object to your Form, and have it update itself accordingly.
BTW: if your option box is showing some initial settings that are persisted in a text file (maybe XML), pass the file path to the Form, and learn it how to adapt to the content of the file; and provide another method to dump its state to that file; etc.
|
|
|
|
|
hmm, yeah haven't thought about that. i think i can create that. Thanks!
|
|
|
|
|
hello frds..........
i m creating a collg project job searching website...
In user table email address of user are stored.
if i want admin of my application to provie afunctionality to send email about job alerts to all users how cn i do it?
is it possible?
at which time i should send an email of registrain confiration?at registration time or it does the admin manually?
If database is so large is it possible to send email to all members using there addresses from data base directaly?
pls help...
tell about any idea related to this...
|
|
|
|
|
There is no problem in sending mails to multiple users. Just retrieve the email id's from database by inserting a comma after each email id AND place the resultant string to 'To' property of MailMessage object.
Happy Coding....
|
|
|
|
|
I have the following two functions:
private void freezeFrame_MouseClick(object sender, MouseEventArgs e)
{
txtMouseX.Text = e.X.ToString();
txtMouseY.Text = e.Y.ToString();
getPixel();
}
private void getPixel()
{
int rangeLimit = 20;
int mouseX = int.Parse(txtMouseX.Text);
int mouseY = int.Parse(txtMouseY.Text);
Bitmap refBitmap = null;
System.Drawing.Image refImage = null;
refImage = freezeFrame.Image;
refBitmap = new Bitmap(refImage);
Color currentPixel = (refBitmap.GetPixel(mouseX, mouseY));
curPixelRed.Text = currentPixel.R.ToString();
curPixelGreen.Text = currentPixel.G.ToString();
curPixelBlue.Text = currentPixel.B.ToString();
redMinUpDown.Value = currentPixel.R - rangeLimit;
redMaxUpDown.Value = currentPixel.R + rangeLimit;
greenMinUpDown.Value = currentPixel.G - rangeLimit;
greenMaxUpDown.Value = currentPixel.G + rangeLimit;
blueMinUpDown.Value = currentPixel.B - rangeLimit;
blueMaxUpDown.Value = currentPixel.B + rangeLimit;
}
when i run the code, and click on the picturebox i get the following error "ArgumentException was unhandeled" with the statement "Parameter is not valid" for the line refBitmap = new Bitmap(refImage). I tried changing up the way i declare the refImage and refBitmap as suggested by people on my previous question and nothing works. What should I do?
When I debug, the Locals are as follows:
rangeLimit = 20
mouseX = 70 (red)
mouseY = 91 (red)
reBitmap = null
refImage = {System.Drawing.Bitmap}
currentPixel = "{Name=0, ARGB=(0,0,0,0)}"
Is there a direct way to convert a System.Drawing.Image to System.Drawing.Bitmap? I tried what someone else instructed me to do and I keep getting the same error and if I ask this question again, people just bash me on the fact that the code is low quality or other non-constructive criticism. All i need is to figure out what is wrong with my code, not a lecture on why i suck as a programmer. We all start somewhere and we all make mistakes but I digress. Can anyone help me solve this dilemma?
|
|
|
|