Reputation: 185
I got this file to That I have to write an nunit test on::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionsLib
{
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public double Salary { get; set; }
public DateTime DOJ { get; set; }
}
public class EmployeeManager
{
private static readonly List<Employee> employees;
static EmployeeManager()
{
employees = new List<Employee>
{
new Employee { EmpId=100, EmpName="John",DOJ=DateTime.Now.AddYears(-5),Salary=30000},
new Employee { EmpId=101, EmpName="Mary",DOJ=DateTime.Now.AddYears(-2),Salary=10000},
new Employee { EmpId=102, EmpName="Steve",DOJ=DateTime.Now.AddYears(-2),Salary=10000},
new Employee { EmpId=103, EmpName="Allen",DOJ=DateTime.Now.AddYears(-7),Salary=50000},
};
}
public List<Employee> GetEmployees()
{
return employees;
}
public List<Employee> GetEmployeesWhoJoinedInPreviousYears()
{
return employees.FindAll(x=>x.DOJ<DateTime.Now);
}
}
}
My question is:
Upvotes: 1
Views: 2469
Reputation: 13726
To test that a list has no null members, you can use several forms of assertion in NUnit, listed in my personal order of preference...
Assert.That(someList, Is.All.Not.Null);
Assert.That(someList, Has.None.Null);
Assert.That(someList, Has.All.Not.Null);
Assert.That(someList, Does.Not.Contain(null);
CollectionAssert.AllItemsAreNotNull(someList);
In each case, someList
is the list you want to test.
If you check the docs at https://docs.nunit.org/articles/nunit/intro.html, you may find other options as well! Take your pick! :-)
Upvotes: 1
Reputation: 2061
It could look like this:
[Test]
public void GetEmployees_Should_NotReturnAnyNulls()
{
var manager = new EmployeeManager();
var employees = manager.GetEmployees();
foreach(var employee in employees)
{
Assert.NotNull(employee);
}
}
Or even better, with the nuget FluentAssertions like this:
[Test]
public void GetEmployees_Should_NotReturnAnyNulls()
{
var manager = new EmployeeManager();
var employees = manager.GetEmployees();
employees.Should().NotContainNulls();
}
Upvotes: 0