异常单元测试

时间:2015-06-21 01:13:50

标签: c# unit-testing visual-studio-2013

我正在编写一个单元测试,通过模型传递,特别是标题字段。

我的测试如下:

  [TestMethod]
    [ExpectedException(typeof(Exception), "Movie Title is mandatory")]
    public void Create()
    {
        var mc = new MoviesController();

        var model = new Movie
        {
            Cast = new[] { "Scott", "Joe", "mark" },
            Classification = "PG",
            Genre = null,
            MovieId = 0,
            Rating = 5,
            ReleaseDate = 2004,
            Title = null
        };

        var rep = mc.Create(model);
    }

通过控制器调用此类:

public int Create(Movie movie)
    {
        int num;
        if (MovieRepository.Movies == null)
        {
            throw new Exception("Movies datasource is not available");
        }
        lock (MovieRepository._dsMovies)
        {
            DataRow str = MovieRepository._dsMovies.Tables["Movie"].NewRow();
            int num1 = MovieRepository._pk + 1;
            MovieRepository._pk = num1;
            int num2 = num1;
            movie.MovieId = num2;
            str["Id"] = num2;
            if (string.IsNullOrEmpty(movie.Title))
            {
                throw new Exception("Movie Title is mandatory");
            }
            str["Title"] = movie.Title.Trim();
            if (!string.IsNullOrEmpty(movie.Genre))
            {
                str["Genre"] = movie.Genre.Trim();
            }
            if (!string.IsNullOrEmpty(movie.Classification))
            {
                str["Classification"] = movie.Classification.ToString();
            }
            str["Rating"] = movie.Rating;
            str["ReleaseDate"] = movie.ReleaseDate;
            MovieRepository._dsMovies.Tables["Movie"].Rows.Add(str);
            if ((movie.Cast == null ? 0 : (int)((int)movie.Cast.Length > 0)) != 0)
            {
                this.AddCast(movie);
            }
            MovieRepository._dsMovies.AcceptChanges();
            num = num2;
        }
        return num;
    }

如您所见,它会检查标题

if (string.IsNullOrEmpty(movie.Title))
        {
            throw new Exception("Movie Title is mandatory");
        }

我可以断言那个例外吗?因为它失败后我点击输出来检查失败日志,我看到了:

  

测试名称:CreateMovie

     

测试结果:失败

     

结果消息:测试方法Project.Test.UnitTest1.CreateMovie   不要抛出异常。属性预计会有例外   Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute   在测试方法上定义。

     

结果StandardOutput:

     

System.Exception:电影标题是强制性的

     

在Movies.MovieRepository.Create(电影电影)

     

在Project.Web.Controllers.MoviesController.Create(电影电影)中   c:\ Users \ Name \ Documents \ Visual Studio   2013 \项目\ Project.Web \ Project.Web \ \控制器MoviesController.cs:行   113

根据结果消息,它表示它没有抛出异常,当我知道它确实/应该

1 个答案:

答案 0 :(得分:0)

你可以将你的单元测试包装在try catch中,并在catch中放入一个调试点,这样你就可以看到它发生了,并轻松调试它:

[TestMethod]
[ExpectedException(typeof(Exception), "Movie Title is mandatory")]
public void Create()
{
    var mc = new MoviesController();

    var model = new Movie
    {
        Cast = new[] { "Scott", "Joe", "mark" },
        Classification = "PG",
        Genre = null,
        MovieId = 0,
        Rating = 5,
        ReleaseDate = 2004,
        Title = null
    };

    try
    {
        var rep = mc.Create(model);
        Assert.Fail("This shouldn't be shown, because the above should have thrown an exception ! !");
    }
    catch (Exception e)
    {
        Console.Out.WriteLine("AH ha! caught it ! :) : " + e);
        throw;
    }        
}

作为额外的奖励,你可以Assert.Fail如果它没有抛出异常就不会超过它。

作为一个额外的奖励,你不应该抛出Exception而是你自己的例外?也许是MovieException,所以你可以专门捕获那个例外。