1

我正在使用MvcContrib-TestHelper在我的應用程序上測試路由。我有僅限於HTTP POST動作:如何測試MVC操作只能通過HTTP POST訪問?

 
public TestController 
{ 
    [HttpPost] 
    public ActionResult Example() 
    { 
     return View(); 
    } 
} 

這裏是一個測試的例子應該失敗:

 
[TestFixture] 
public class RoutingTests 
{ 
    [TestFixtureSetUp] 
    public void TestFixtureSetUp() 
    { 
     RouteTable.Routes.Clear(); 
     Application.RegisterRoutes(RouteTable.Routes); 
    } 

    [Test] 
    public void TestWithGet() 
    { 
     var route = "~/Test/Example".WithMethod(HttpVerbs.Get); 
     route.ShouldMapTo(r => r.Example()); 
    } 
} 

然而,測試通過!我已經看到了另一個未答覆的問題(對不起,錯誤的鏈接),這也引發了這種問題,而且看起來功能已經被破壞了。測試此路線是否僅通過POST可訪問的更好方法是什麼?

回答

1

使用此代碼:

var controller = new HomeController(); 
var methodInfo = controller.GetType().GetMethod("MrthodName"); 
var attributes = methodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true).Cast<ActionMethodSelectorAttribute>().ToList(); 

屬性 - 這是列表接受動詞

+0

無論[HttpPost]和[AcceptVerbs(HttpVerbs.Post)]之間的區別如何,它都可以工作嗎? – sargant

+0

檢查出來。我幾乎90%確定這是相同的 – Mediator

+0

鑑於它的測試,它分別看到AcceptVerbsAttribute和HttpPostAttribute。反射可能就像我會得到的那樣 - 我會看看是否有更接近實際分析路由信息的方式,如果沒有,我很快就會接受這個答案。謝謝! – sargant

2

它看起來像你只是試圖在那裏測試ASP.NET MVC框架。我不認爲這樣的測試將帶來價值......

+0

更好的爲註釋 – wal

+2

他可以確保某些動作只有可通過郵寄獲得。 – dove

+0

是的,你是對的。然而這取決於上下文。我認爲在大多數情況下這些測試是不相關的 – CoffeeCode