![](/img/trans.png)
[英]ASP.NET 5 - MVC 6 - Unit Test a Controller that uses Url.Action
[英]Unit test a controller attribute which depends on the requested URL parameters (ASP.NET MVC)
我基本上有一個控制器基類,它通過驗證url中的令牌來處理身份驗證,例如:
http:// example / Home / Index將未經身份驗證
http:// example / Home / Index?token = 293029420932422823將通過身份驗證
我想為此編寫單元測試,但不確定如何去做。 我有:
[TestMethod]
public void NonsecureConnection()
{
var controller = new EONSecureController();
Assert.IsTrue(string.IsNullOrEmpty(controller.EONToken));
Assert.IsTrue(controller.tokenMode == EONSecureController.EONTokenModeEnum.None);
}
很簡單。 測試安全連接是我遇到的困難。
[TestMethod]
public void SecureConnection()
{
var controller = new EONSecureController();
// What to put here?
Assert.IsTrue(!string.IsNullOrEmpty(controller.EONToken));
Assert.IsTrue(controller.tokenMode==EONSecureController.EONTokenModeEnum.Client);
}
我嘗試了各種方法,例如:
controller.RequestContext.RouteData.Values[Config.RawTokenName] = "12312342123";
controller.Request = new System.Web.HttpRequest("", "http://localhost/example", "token=2342342");
controller.Request.RawUrl = "http://localhost/example?token=234234234234234";
由於各種原因,它們都不起作用。 是否有“正確”的方式來處理依賴於所請求的特定URL的控制器測試?
我已經看到一些暗示要使用Moq的東西,但我還不能完全理解...
注意 :請不要按照“不要那樣做身份驗證,而應該{etc etc etc}來”建議答案。 有意簡化了該示例,以重點關注此處的主要問題,即如何開發單元測試。
單獨測試屬性-不能與控制器一起測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.