I am writing Unit tests for controller class using XUNIT. Here is my code.
private Mock<StudentService> _StudentService;
private StudentController StudentController;
[Fact]
public void GetStudentsBySubject_ShoutReturnNotNull()
{
List<StudentViewModel> Students = new List<StudentViewModel>();
_studentService = new Mock<StudentService>();
string Subject = "Math";
_StudentService.Setup(x => x.GetStudentsBySubject(Subject))
.Returns((Students));
var controller = new StudentController(_StudentService.Object);
var result = controller.GetStudentsBySubject(Subject);
Assert.IsType<List<StudentViewModel>>(result);
}
It throws error at line.
_StudentService.Setup(x => x.GetCheapestStudentsBySubject(Subject))
.Returns((Students));
Message: System.NullReferenceException : Object reference not set to an instance of an object
I tried many changes. But nothing worked. What could I be doing wrong here.
The following error appears after fixing the nullreference error:
System.NotSupportedException : Unsupported expression: x => x.GetStudentsBySubject(StudentControllerTests.<>c__DisplayClass2_0.Subject) Non-overridable members (here: StudentService.GetStudentsBySubject) may not be used in setup / verification expressions