0

私は初心者で、これまで単体テストを行ったことがありません。

ファイルを圧縮して電子メールを送信するコンソール アプリケーションを作成しました。今、私はユニットテストをしたいと思っています。しかし、私のコードがテスト可能かどうかはわかりません。

たとえば、私は - と呼ばれるメソッドを持っています

 public  static void readAndEmailCsvFiles(string filePath)
        {
            DirectoryInfo directory = new DirectoryInfo(filePath);
            var files = directory.GetFiles("*.csv", SearchOption.AllDirectories);
            var dirDate = string.Format("{0:yyyy-MM-dd HH-mm}", DateTime.Now);
            bool isExists = System.IO.Directory.Exists(filePath + "\\" + "PROCESSED" + "\\" + dirDate);

            if (!isExists)
            {
                System.IO.Directory.CreateDirectory(filePath + "\\" + "PROCESSED" + "\\" + dirDate);
            }
            try
            {
                using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
                {

                    foreach (var file in files)
                    {
                        Console.WriteLine("Processing File : " + file + "\n");
                        zip.AddFile(file.FullName, "");
                        zip.Save(Path.Combine(filePath, "PROCESSED", dirDate, file.Name) + ".zip");
                        sendEmail.SendMailMessage(Path.Combine(filePath, "PROCESSED", dirDate, file.Name) + ".zip");

                    }


                }

                foreach (var file in files)
                {
                    File.Delete(file.FullName);
                    Console.WriteLine("");
                }


            }               

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


        }

上記のメソッドのテストを作成するにはどうすればよいですか?

4

3 に答える 3

7

あなたのクラスはあまりにも多くの責任を所有しています:

  • ファイルのリストをフェッチするロジック
  • ファイルを圧縮するロジック
  • メールを送信するロジック
  • 基本的にこれがワークフローを形成します

あなたへの最初の提案は、これらの責任をさまざまなサービス (クラス) に分割し、これらのサービスをワークフローに挿入し、各サービスを個別にテストし、最後にこれらすべてのサービスを使用して仕事を完了するワークフローをテストすることです。

例:

  • 指定されたファイルに電子メールを送信する EmailService クラスを持たせる

  • パスなどを指定してファイルのリストを取得する FileRepository クラスを用意する

これらのサービスを注入する 1 つの可能な方法は、次のようなものです。

public class ClassName  
{  
    public ClassName(IEmailService emailService, IFileRespository fileRepository)  
    {  
        // You might want store the reference to these injected services 
        // and later use them to perform useful work 
    }  
    public void DoSomething()
    {
        // Do Something useful
    }
}
于 2013-11-05T05:06:53.253 に答える
0

As the other answers below split the logic to two methods.

  • Method1: To create the Gzip
  • Method2: TO mail the file.

The Method1 can either return the zipped file or write it to a folder. Then you could unit test it like this:

  • Test1 Check whether file exists
  • Test2 Check whether file is in the Correct Format (try to unzip with the same logic inside the unit test method)

The Method2 can also be unit tested:

The link says how to unit test a mail sending functionality. All we need to do is to configure the web.config inorder to unit test the mail functionality

于 2013-11-05T05:24:25.380 に答える