2009-06-15 59 views
2

我已經註冊了2條路線如下單元測試MVC路線是POST

[Test] 
public void CanVerifyRouteMaps() 
    { 
     "~/".Route().ShouldMapTo<HomeController>(x => x.Index()); 
    } 

我知道GetAnEmail工作,但一個單元如何測試POST過的路線?

回答

2

Gratzy的答案是接近的,但所有顯示我是如何通過代碼做POST。

我認爲解決方案暗示在Stephen Walther's blog post。我真的在測試我的路線約束,這是關鍵。 Stephen在他的例子中創建了一個僞造的httpContext。我會試着用Rhino Mocks來做這件事,一旦我有一個工作的例子,就會回來。如果有人已經使用Rhino Mock或Moq完成了這項工作,請發佈。

+0

嗨,你有沒有解決這個問題? – rohancragg 2011-01-10 16:47:51

1

您需要模擬單元測試中的帖子。

System.Net.WebRequest req = System.Net.WebRequest.Create("your url"); 

req.ContentType = "text/xml"; 
req.Method = "POST"; 

byte[] bytes = System.Text.Encoding.ASCII.GetBytes("Your Data"); 
req.ContentLength = bytes.Length; 
os = req.GetRequestStream(); 
os.Write(bytes, 0, bytes.Length); 


System.Net.WebResponse resp = req.GetResponse(); 
if (resp == null) return; 
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()); 

str responsecontent = sr.ReadToEnd().Trim();