views:

163

answers:

1

Hi all,

I am in the process of taking over a legacy system that contains a Console App project, which is run as a Scheduled Task on our production servers. It mainly produces daily and weekly reports, does some database maintenance, etc.

The "main" of the console app handles inputting the command line arguments and determining which of several different processes to execute. Something like

Module MainModule

Public Sub Main()
    '--- Check if command line arguments were specified
    Dim args() As String = Environment.GetCommandLineArgs()
    If args.Length > 1 Then
        ConsoleMain(args)
    End If
End Sub

Public Sub ConsoleMain(ByVal args() As String)
    Dim rc As New Coordinator(enableEmails_)

    Try
        Dim arg_ As String = args(1)
        Dim success_ As Boolean = True

        Select Case arg_.ToUpper()
            Case TaskType.OrderIntegration
                success_ = rc.OrderIntegration()
            Case TaskType.Motivators
                success_ = rc.MotivatorsCreateFile(New CustomerMotivatorsManager)

       ... repeat for each of the various "Task Types"

End Module

What my question is: - This being a Console App with a Main() and ConsoleMain(), I don't seem to have anything that I can access from a Test - i.e. the "Main" and "ConsoleMain" do not appear to be accessible. How can I unit-test something like this, to test the "if argument 'x' is passed, function 'y' is called"?

Thanks in advance,

A: 

I'm not sure why Main wouldn't be visible from your tests, unless VB.NET does some behind-the-curtains stuff to hide it.

In any case, why not move your code into its own class(es)? Then you can run unit tests against each class at a time, rather than executing the whole thing at once.

Unit tests usually execute against individual classes, rather than executing the Main entry point of an app.

Judah Himango
It appears that it's related to the Main being in a "Module" instead of a "class". The items in other Classes in the project are visible, just nothing inside this main "Module".I was hoping to avoid refactoring this very much, as it is a highly visible production job. However, the extraction of the main logic into its own class (that takes the command line args as a parameter) is a possibility. Thanks!
jim gilbert
Cool. Since this led you in the right direction, are you going to mark this as the answer? (Or at least give me an upvote?) Appreciate it!
Judah Himango