November-06's Profile User Rating: *****

Reputation: 46 Craftsman
Group:
Active Members
Active Posts:
408 (0.28 per day)
Joined:
04-January 11
Profile Views:
15,592
Last Active:
User is offline Dec 17 2014 06:34 AM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Icon   November-06 has not set their status

Posts I've Made

  1. In Topic: TDD: How to correctly implement?

    Posted 27 Nov 2014

    If, for example, I have a method that computes for the factorial of a number...

    
    public int GetFacorial(int number)
    {
        int factorial = 1;
    
        while (number > 1)
        {
            factorial *= number;
            number -= 1;
        }
        return factorial;
    }
    
    
    


    To test, I assert the expected result to the actual result...

    
    [TestMethod]
    public void GetFactorialCorrectly()
    {
        frmWordGuess wordGuessForm = new frmWordGuess();
        Assert.AreEqual(2, wordGuessForm.GetFacorial(2));
    }
    
    
    


    If, for example, I don't only get the value of the factorial. Instead, I list the result of every loop in a listbox. Do I test every item added for every loop?

    How is the test method done? Do you also place a looping statement inside the test method?

    I hope you could give me an example of how to test loops.
  2. In Topic: TDD: How to correctly implement?

    Posted 26 Nov 2014

    Quote

    Quote

    The method has a condition but result determines the dynamic addition of a control in the UI


    Then test that the control has been added.


    I am thinking that to unit test a method that dynamically creates multiple controls in a panel using loop, I should have my method return how many controls are added instead of checking control names one by one.

    Then, I can assert how many controls I am expecting to be added compared to the actual no. of controls added.

    One example I can think of is creating buttons for each alphabet.

    Is this right? Or, can you recommend a better approach?
  3. In Topic: TDD: How to correctly implement?

    Posted 25 Nov 2014

    Thanks. Now, I am beginning to understand the difference between unit testing and TDD.

    I have made a simple practice application. Below is one of the methods in it. Could you guide me on how should I have unit tested it?

    
    private void LoadLetterHint(string secretWord)
    {
       int leftPosition = 0;
       pnlSecretWordHint.Controls.Clear();
    
       foreach (char letter in secretWord)
       {
          Label lblLetterHint = new Label();
          lblLetterHint.BorderStyle = BorderStyle.FixedSingle;
          lblLetterHint.BackColor = Color.Beige;
          lblLetterHint.Width = 20;
          lblLetterHint.Height = 20;
          lblLetterHint.Left = leftPosition;
          lblLetterHint.Font = new Font(lblLetterHint.Font.FontFamily, 10);
    
          WordGuess wordGuess = new WordGuess();
          if (wordGuess.CorrectLetterGuess(letter, userGuessedLetters))
             lblLetterHint.Text = letter.ToString().ToUpper();
    
          pnlSecretWordHint.Controls.Add(lblLetterHint);
          leftPosition += 30;
       }
    }
    
    
    


    This is a simple application of guessing the word in three tries. What this method does is display a hint of the secret word by adding label controls in the form.

    For example:

    Secret Word is EDITOR

    Method displays 6 labels to represent the 6 characters of the secret word, EDITOR. So display is like _ _ _ _ _ _ , where the underscores represents each label.

    The WordGuess is my class which contains the functions that check if userInput is correct. The functions in this class like the "CorrectLetterGuess" already have unit test.

    If result of the "CorrectLetterGuess" method is true, then the label will show the value of the letter. So if user has already guessed "O", display will be _ _ _ _ O _

    "userGuessedLetters" is a global variable of type List<char>


    In this sample, what should I assert? Do I assert for every loop?
  4. In Topic: TDD: How to correctly implement?

    Posted 25 Nov 2014

    There are many articles in the internet that discusses which methods to and to not unit test.

    I remember reading about not unit testing for methods that generate random numbers. Also, I have read that if you are unit testing a method that calls different methods, then better unit test the methods being called by it. And the more frequent discussed in articles is... "you don't unit test a method with no logic". This includes if...else... statements, switch...case..., loops and other codes that may involve decision-making.

    Well, I don't really expect every internet article to be discussing the same thing since they come from different sources and different developers may have different ideas.

    But now, I'm confused. It seems that you need to test every method.

    Correct me if I am wrong. Here's how I think it works...

    If you unit test methods but with the exception of some methods (like what I did), you are unit testing but not using TDD.

    If you use TDD, you need to have a test for all methods. In a test first approach, no method will be created without test.

    Do I understand it right?

    Does this mean that in TDD, there are no exceptions that tells that the method does not need to be tested?
  5. In Topic: TDD: How to correctly implement?

    Posted 25 Nov 2014

    I am able to complete the simple application I am working on. I am not sure though if I am able to perform enough unit testing. Compared to the many methods that I have in my application, I have only a few methods which I include in my test.

    Below are the reasons that I have for not creating a test for a certain method: (Please guide me by telling me if you agree or disagree, and if my reason is valid or invalid)
    • The method does not have any conditions for decision-making
    • The method simply displays something on a label or textbox or any other object
    • The method calls other methods (1 or more)
    • The method has a logical condition but output is displayed in a UI object(textbox/label/MessageBox/etc). (cfoley, should I be using your suggestion for testing methods with no return value here?)
    • The method has a condition but result determines the dynamic addition of a control in the UI
    • The method is returning a random value
    • The condition determines how properties of a control will be set (Enabled, Checked, MaxLength, etc.)

My Information

Member Title:
D.I.C Regular
Age:
24 years old
Birthday:
October 17, 1990
Gender:
Interests:
Programming, Playing Chess
Programming Languages:
C#.NET
VB.NET
ASP.NET
LINQ
VB 6.0
Java
C
MATLAB

Contact Information

E-mail:
Private
Yahoo:
Yahoo  sixth_of_november@yahoo.com

Comments

November-06 has no profile comments yet. Why not say hello?