关于c#:异常单元测试

unit test on exception

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

我的测试如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  [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);
    }

通过控制器调用此类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
    }

正如您所看到的,它会检查标题

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

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

Test Name: CreateMovie

Test Outcome: Failed

Result Message: Test method Project.Test.UnitTest1.CreateMovie did
not throw an exception. An exception was expected by attribute
Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute
defined on the test method.

Result StandardOutput:

System.Exception: Movie Title is mandatory

at Movies.MovieRepository.Create(Movie movie)

at Project.Web.Controllers.MoviesController.Create(Movie movie) in
c:\Users
ame\Documents\Visual Studio
2013\Projects\Project.Web\Project.Web\Controllers\MoviesController.cs:line
113

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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[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,那么你可以特别抓住那个例外。