我是编写单元测试用例的新手。我在User.Identity
收到错误。我看到嘲笑是解决这个问题,我试过这个问题并不适用于我的情况。我添加了我的代码
我的控制器
public ActionResult CreateStage ( EnthiranStageViewModel enthiranStage )
{
if ( ModelState.IsValid )
{
Stage stage = enthiran.Insert_Stage(enthiranStage);
//logging Stage Creation
util.ApplicationLog(new ViewModel.Logs.ApplicationLogViewModel
{
GameCategorys = GameCategory.Enthiran,
Event = Events.EnthiranStageCreation,
SessionAttemptId = null,
UserId = User.Identity.GetUserId<int>( ),
OptionalParameter1 = enthiranStage.GameId,
OptionalParameter2 = stage.Id,
Description = "Enthiran stage created"
});
return RedirectToAction("Stages", new
{
id = stage.GameId
});
}
return View( );
}
以下是我的测试用例
[TestMethod( )]
public void createStage ( )
{
EnthiranStageViewModel enthiranStage = new EnthiranStageViewModel
{
StageType=0,
TriggerBeginType = Akton.Areas.Challenge.Models.TriggerType.Manual,
TriggerEndType= Akton.Areas.Challenge.Models.TriggerType.Manual,
TimeLimit = new TimeSpan(9, 6, 13),
TriggerBeginTime= new DateTime(2016, 09, 3, 9, 6, 13),
TriggerEndTime= new DateTime(2016, 09, 3, 9, 6, 13),
StartValueType= Akton.Areas.Challenge.Models.StartValueType.Global,
StageDate= new DateTime(2016, 09, 3, 9, 6, 13),
Proforma=25,
GameId=19,
CreatedTime=new DateTime(2016, 09, 3, 9, 6, 13),
UpdatedTime= new DateTime(2016, 09, 3, 9, 6, 13),
StageName="Test",
};
EnthiranController controller = new EnthiranController( );
JsonResult actual = controller.CreateStage(enthiranStage) as JsonResult;
var result = actual.Data;
Assert.AreEqual("{ success = True }", result.ToString( ));
}
在这里,我必须传递userId
中的ViewModel.Logs.ApplicationLogViewModel
,我不知道该怎么做。
如何获得通过userId
的{{1}}?
答案 0 :(得分:2)
一种解决方案是更改EnthiranController
并传递,例如IUserContext
,如下所示:
public interface IUserContext
{
public IPrincipal User {get;}
}
然后通过构造函数将其传递给控制器,并使用该上下文来检索用户。
ctor EnthiranController(IUserContext userContext)
然后稍微更改单元测试以模拟所有这些接口。另外,JsonResult
代替ActionResult
,您可以使用RedirectToRouteResult
或[TestMethod( )]
public void createStage ( )
{
//arrange
EnthiranStageViewModel enthiranStage = new EnthiranStageViewModel
{
StageType=0,
TriggerBeginType = Akton.Areas.Challenge.Models.TriggerType.Manual,
TriggerEndType= Akton.Areas.Challenge.Models.TriggerType.Manual,
TimeLimit = new TimeSpan(9, 6, 13),
TriggerBeginTime= new DateTime(2016, 09, 3, 9, 6, 13),
TriggerEndTime= new DateTime(2016, 09, 3, 9, 6, 13),
StartValueType= Akton.Areas.Challenge.Models.StartValueType.Global,
StageDate= new DateTime(2016, 09, 3, 9, 6, 13),
Proforma=25,
GameId=19,
CreatedTime=new DateTime(2016, 09, 3, 9, 6, 13),
UpdatedTime= new DateTime(2016, 09, 3, 9, 6, 13),
StageName="Test"
};
Mock<IPrincipal> mockPrincipal = new Mock<IPrincipal>();
//TODO: setup mockPrincipal
Mock<IUserContext> mockUserContext = new Mock<IUserContext>();
mockUserContext.Setup(p => p.User).Returns(mockPrincipal.Object);
EnthiranController controller = new EnthiranController(mockUserContext.Object);
//act
var actual = controller.CreateStage(enthiranStage) as RedirectToRouteResult;
//assert
Assert.IsNotNull(actual);
}
,如下例所示。
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './component/home.component';
import { RegisterComponent } from './component/register.component';
import { ProductComponent } from './component/product.component';
const appRoutes: Routes = [
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: 'home',
component: HomeComponent
},
{
path: 'register',
component: RegisterComponent
},
{
path: 'product',
component: ProductComponent
}
];