2016-05-09 25 views
0

私はAsp.NET MVC Adminエリアのルートをテストしようとしています。ここで Asp.NET MVC TelerikジャストモックLiteとのArearegistrationルートユニットテスト

は、私はコードをしようとしている:

 [TestMethod] 
    public void AdminRouteUrlIsRoutedToHomeAndIndex() 
    { 
     //spts.saglik.gov.tr/admin 
     //create route collection 
     var routes = new RouteCollection(); 

     var areaRegistration = new AdminAreaRegistration(); 
     Assert.AreEqual("Admin",areaRegistration.AreaName); 

     // Get an AreaRegistrationContext for my class. Give it an empty RouteCollection 
     var areaRegistrationContext = new AreaRegistrationContext(areaRegistration.AreaName, routes); 
     areaRegistration.RegisterArea(areaRegistrationContext); 

     // Mock up an HttpContext object with my test path (using Moq) 
     var context = Mock.Create<HttpContext>(); 
     context.Arrange(c=>c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Admin"); 

     // Get the RouteData based on the HttpContext 
     var routeData = routes.GetRouteData(context.Request.RequestContext.HttpContext); 

     //assert has route 
     Assert.IsNotNull(routeData,"route config"); 

    } 

var context = Mock.Create<HttpContext>();だけのモックだから私はtelerikちょうどモックLiteでエリア登録ルートのユニットテストを行うことができますどのようにこのエラー

Telerik.JustMock.Core.ElevatedMockingException: Cannot mock 'System.Web.HttpContext'. JustMock Lite can only mock interface members, virtual/abstract members in non-sealed classes, delegates and all members on classes derived from MarshalByRefObject on instances created with Mock.Create or Mock.CreateLike. For any other scenario you need to use the full version of JustMock.

を告げますか?どうすればこの問題を解決できますか?

ありがとうございます。

答えて

0

HttpContextはあなたがモックできないものです。それに含まれるデータは、特定の要求に固有です。したがって、HttpContextを使用してテストを実行するには、リクエストを行うことができる環境で実際にアプリケーションを実行する必要があります。

代わりに、MvcRouteTest(https://github.com/AnthonySteele/MvcRouteTester)のようなサードパーティのツールを使用する必要があります。使い方は簡単ですが、最も重要なことは、アプリを実行しなくてもテストを実行できることです。

[TestMethod] 
public void AdminRouteUrlIsRoutedToHomeAndIndex() 
{ 
    var routes = new RouteCollection(); 

    var areaRegistration = new AdminAreaRegistration(); 
    Assert.AreEqual("Admin", areaRegistration.AreaName); 

    var areaRegistrationContext = new AreaRegistrationContext(areaRegistration.AreaName, routes); 
    areaRegistration.RegisterArea(areaRegistrationContext); 

    routes.ShouldMap("/admin").To<HomeController>(r => r.Index()); 
} 

これは、エリア登録と/ admin URLのルートの両方をテストします(2つのテストに分割する必要があります)。それはあなたのAdminAreaRegistrationRegisterArea()方法は、ホームとして設定されたデフォルトのコントローラとデフォルトルートを構築することを前提としています

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "Admin_default", 
     "Admin/{controller}/{action}/{id}", 
     new { controller="home", action = "Index", id = UrlParameter.Optional } 
    ); 
} 
+0

私はそのまたtest.Doがregisterareaあなたがいずれかを持っている、これはそのだけではなく、ルートテストをalso.But使用していますこれのコード例? – kodcu

関連する問題