Reputation: 15
I have an N-tier project that am trying to test the API controller but the flow is a little bit more complex which makes me ask for some opinion from you guys... Please any suggestion will be appreciated so much...
I'm getting an error
Object reference not Set to An Instance Of An Object
This is my test class and method that get all fake symptoms
[TestClass]
public class SymptomsControllerTest
{
private Mock<IAdministrationManagementService> _administrationManagementServiceMock;
SymptomsController objController;
IList<SymptomObject> listSymptoms;
[TestInitialize]
public void Initialize()
{
_administrationManagementServiceMock = new Mock<IAdministrationManagementService>();
objController = new SymptomsController(_administrationManagementServiceMock.Object);
listSymptoms = new List<SymptomObject>() {
new SymptomObject() { Name = "Head1" },
new SymptomObject() { Name = "Head2" },
new SymptomObject() { Name = "Head3" }
};
}
[TestMethod]
public void Symptom_Get_All()
{
//Arrange
_administrationManagementServiceMock.Setup(x => x.GetSymptoms()).ReturnsAsync(listSymptoms);
//Act
var result = objController.Get() as List<SymptomObject>;
//Assert
Assert.AreEqual(result.Count, 3);
Assert.AreEqual("Head1", result[0].Name);
Assert.AreEqual("Head2", result[1].Name);
Assert.AreEqual("Head3", result[2].Name);
}
}
the service am trying to communicate to looks likes this
public Task<IList<SymptomObject>> GetSymptoms()
{
return Task.Run(() =>
{
try
{
using (var _uow = new HomasUoW())
{
var entities = _uow.Symptoms.GetAll().Where(x => x.isDelete == false);
if (entities.Count() > 0)
{
IList<SymptomObject> list = new List<SymptomObject>();
foreach (var entity in entities)
{
var obj = AutoMapper.Mapper.DynamicMap<Symptom, SymptomObject>(entity);
obj.Name = entity.Name;
list.Add(obj);
}
return list;
}
else
{
throw new InvalidOperationException(Resources.NonExistingObjectRetrievalError);
}
}
}
catch (Exception)
{
throw;
}
});
}
and the API Controller looks like this
public IHttpActionResult Get()
{
try
{
var symptoms = _Service.GetSymptoms();
if (symptoms != null)
{
return Ok(symptoms);
}
return NotFound();
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
Please look at it carefully and check if am missing any thing that is not allowing the test to pass.
Upvotes: 0
Views: 2548
Reputation: 218827
Based on your comment:
the null exception is coming from this line of code
Assert.AreEqual(result.Count, 3);
Clearly the result
object is null
. Look at how you get that object:
var result = objController.Get() as List<SymptomObject>;
Even if Get()
returns something, the behavior of the as
keyword is such that when you try to interpret an object as
the wrong type, the result will be null
. You're trying to interpret that object as List<SymptomObject>
. But what does Get()
return?
public IHttpActionResult Get()
You can't cast IHttpActionResult
to List<SymptomObject>
. You have two options:
List<SymptomObject>
(which I would say is probably the better approach, but there are more considerations to be made outside the scope of the code shown), or;IHttpActionResult
being returned.Upvotes: 1