我抓我的頭有點試圖找出如何測試像下面這樣: 這裏是GetProjects方法。如何在NUnit中傳遞UserId?
[Authorize]
[HttpGet]
[Route("api/UserProject/GetUserProjects")]
public HttpResponseMessage GetProjects()
{
string userId = User.Identity.GetUserId();
if (!ModelState.IsValid)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
try
{
List<ProjectDto> userProjectsDtos = projectBll.GetProjects(userId);
return Request.CreateResponse(HttpStatusCode.OK, userProjectsDtos);
}
catch (Exception)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
我在這裏試圖運行NUnit測試了該方法。
[TestClass]
public class TestUserProjects
{
[TestMethod]
public void GetUserProjectsTest()
{
// Arragnge
var controller = new UserProjectController();
controller.Request = new HttpRequestMessage();
controller.Configuration = new HttpConfiguration();
foreach (var testUserId in GetTestUserIds())
{
// Act
var response = controller.GetProjects();
// Assert
List<ProjectDto> projects;
Assert.IsTrue(response.TryGetContentValue<List<ProjectDto>>(out projects));
//Assert.AreEqual(5, projects.Count); // Check projects count
Assert.AreEqual(HttpStatusCode.OK,response.StatusCode);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode,"GetUserProjects : id is {0} and count is {1}",testUserId,projects.Count);
System.Diagnostics.Debug.WriteLine("GetUserProjects : for id {0} ProjectCount = {1}", testUserId, projects.Count);
}
}
}
我試圖通過用戶id,但不知道從哪裏寫。
這解決了我的問題。現在我不需要更改已經寫入的代碼。 –