Sono nuovo di mvc4 e anche TDD.MVC4 TDD - System.ArgumentNullException: il valore non può essere nullo.
Quando provo a eseguire questo test fallisce, e non ho idea del perché. Ho provato così tante cose che sto iniziando a girare in tondo.
// GET api/User/5
[HttpGet]
public HttpResponseMessage GetUserById (int id)
{
var user = db.Users.Find(id);
if (user == null)
{
//return Request.CreateResponse(HttpStatusCode.NotFound);
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return Request.CreateResponse(HttpStatusCode.OK, user);
}
[TestMethod]
public void GetUserById()
{
//Arrange
UserController ctrl = new UserController();
//Act
var result = ctrl.GetUserById(1337);
//Assert
Assert.IsNotNull(result);
Assert.AreEqual(HttpStatusCode.NotFound,result.StatusCode);
}
E i risultati:
Test method Project.Tests.Controllers.UserControllerTest.GetUserById threw exception:
System.ArgumentNullException: Value cannot be null. Parameter name: request
Usa 'Passo sopra' nel vostro debugger e inserire il metodo, qualcosa deve essere' null' – LukeHennerley
Su un lato nota, unit test non dovrebbero mai accedere nessuno statica risorse come db's. Dovresti iniettare queste dipendenze. Cosa succede quando il tuo db cambia? il tuo test di unità è inutile! – Liam
Direi che db è null o db.Users è nullo. utilizzare il debugger per verificare –