Reputation: 12745
I have a test class and initializing it with test method. I have also setup method returns with mock data. However , when I pass invalid arguments like , null, I should get back a null exception , as implemented in my class.
Instead I get 0 results and not a null exception.
[TestInitialize]
public void Initialize()
{
_mySSystemHealthRepo= new Mock<ImySSystemHealthRepo>();
//Arrannge
List<JobHistory> allJobHistory = new List<JobHistory>
{
new JobHistory
{
Id = 2,
StartTime = DateTime.Parse("15 OCT 2017"),
EndTime = DateTime.Parse("18 OCT 2017"),
Engineer = [email protected]"
Name="Job1"
}
,
new JobHistory
{
Id = 3,
StartTime = DateTime.Parse("12 OCT 2017"),
EndTime = DateTime.Parse("14 OCT 2017"),
Engineer = "[email protected]"
Name="Job1"
},
new JobHistory
{
Id = 4,
StartTime = DateTime.Parse("11 OCT 2017"),
EndTime = DateTime.Parse("16 OCT 2017"),
Engineer = "[email protected]"
Name="Job1"
}
};
_ziSystemHealthRepo.Setup(obj => obj.GetJobHistoryByName(DateTime.Parse("01 OCT 2017"), DateTime.Parse("30 OCT 2017"), "TestJob")).Returns(allJobHistory);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Repo_GetJobHistoryByNames_PassAllNull_ArgumenNullException()
{
ImySSystemHealthRepo repo = _mySSystemHealthRepo.Object;
//Act
var allJobs = repo.GetJobHistoryByName(default(DateTime), default(DateTime), null);
//Assert
}
And my actual implementation goes like this.
public IEnumerable<JobHistory> GetJobHistoryByName(DateTime startDate, DateTime endDate,string jobName)
{
if (String.IsNullOrEmpty(jobName))
{
throw new ArgumentNullException("jobName");
}
var jobDetails = _context.JobHistories.Where(jh => jh.StartTime >= startDate && jh.EndTime <= endDate);
jobDetails = jobDetails.Where(jh=>jh.Name == jobName);
return jobDetails;
}
Edit:
If I am not using MOQ and passing context as null, following methods would fail.
[TestMethod]
public void Repo_GetJobHistoryByNames_PassValid_GetAllRecordsBack()
{
mySYSSystemHealthRepo repo = new mySYSSystemHealthRepo(null);
//Act
var allJobs = repo.GetJobHistoryByName(DateTime.Parse("01 OCT 2017"), DateTime.Parse("30 OCT 2017"), "TestJob");
var allEngineers = allJobs.Select(x => x.Engineer);
//Assert
Assert.AreEqual(3, allJobs.Count());
Assert.IsTrue(allEngineers.Contains("supp"));
}
Upvotes: 0
Views: 496
Reputation: 247333
Create an instance of the subject under test. Inject the mocked context and any other dependencies then call the actual method under test. Based on the method shown, the null test should then work
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Repo_GetJobHistoryByNames_PassAllNull_ArgumenNullException() {
//Arrange
ImySSystemHealthRepo repo = new MyActualSystemHealthRepo(context: null);
//Act
var allJobs = repo.GetJobHistoryByName(default(DateTime), default(DateTime), null);
//Assert
//...N/A
}
In the second test case, the context dependency will need to be mocked to return the fake data and then injected into the subject under test.
[TestMethod]
public void Repo_GetJobHistoryByNames_PassValid_GetAllRecordsBack() {
//Arrange
var mockContext = new Mock<IMyContext>();
mockContext.Setup(_ => _.JobHistories).Returns(allJobHistory);
var repo = new mySYSSystemHealthRepo(mockContext.Object);
//Act
var allJobs = repo.GetJobHistoryByName(DateTime.Parse("01 OCT 2017"), DateTime.Parse("30 OCT 2017"), "TestJob");
var allEngineers = allJobs.Select(x => x.Engineer);
//Assert
Assert.AreEqual(3, allJobs.Count());
Assert.IsTrue(allEngineers.Contains("supp"));
}
The context when called will return the fake collection and the linq expressions should enumerate it.
Upvotes: 2