2009-07-30 34 views
7

Trong một thử nghiệm đơn vị của tôi, tôi muốn kiểm tra xem tất cả các phương thức công khai có trả về kiểu ActionResult hay không. Dưới đây là phương pháp thử nghiệm của tôi:RuntimeType thay vì Type in C# Unit Test

[TestMethod] 
    public void Public_Methods_Should_Only_Return_ActionResults() 
    { 

     MethodInfo[] methodInfos = typeof(MyController).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); 

     foreach (MethodInfo methodInfo in methodInfos) 
     { 
      Assert.IsInstanceOfType(methodInfo.ReturnType, typeof(System.Web.Mvc.ActionResult)); 

     } 

    } 

thử nghiệm này thổi lên trên phương pháp đầu tiên từ MyController:

[Authorize] 
public ActionResult MyList() 
{ 
    return View(); 
} 

Với các lỗi sau:

Assert.IsInstanceOfType failed. Expected type:<System.Web.Mvc.ActionResult>. Actual type:<System.RuntimeType>. 

Khi tôi đặt một breakpoint trên Khẳng định rằng và kiểm tra methodInfo.ReturnType nó thuộc kiểu Type và nó là ActionResult.

Bất cứ ai có thể giải thích cho tôi lý do tại sao bài kiểm tra bị thổi phồng và phải làm gì để làm cho nó hoạt động?

Cảm ơn trước, MR

Trả lời

11

Sử dụng Assert.AreEqual thay vì Assert.IsInstanceOfType. Bạn muốn kiểm tra loại kết quả, không phải loại thông tin kiểu được phản ánh.

+0

Cảm ơn bạn rất nhiều. Bây giờ nó hoạt động như tôi muốn nó. –

Các vấn đề liên quan