lundi 29 juin 2015

Any point unit testing methods that use EF or Linq?

I am writing unit tests for my service layer, and i completely see the point of creating unit tests to validate logic. For example, if i create a function that adds two numbers, sure write a unit test for this.

But if in my service layer method i have something like this

public ICollection<MyEntity> GetAllAsync()
{
    return _context.MyEntities
        .Where(e => !e.IsDeleted)
        .ToList();
}

What is the point in unit testing this? Since i am getting this from a database, it seems stupid to mock the database, because i am then just assuming that Linq is working as it should be?

Would it not be better to test this against an actual "test" database with sameple data in it. This way i can see if the number of record that are retrieved from the database match what i would expect?

I know that testing against a database, makes this more of a integration test, but is it really valid for unit testing?

What if i take another example, say this

public int Delete(long id)
{
    _context.Database.ExecuteCommand("DELETE FROM myTable WHERE Id = ?", id);

    return _context.SaveChanges();
}

How can this function be unit tested? If i mock _context.Database and create a unit test that checks if _context.SaveChanges is being called (which i see no point in what so ever), there is no guarntee that it will actually delete my data. WHat if i have a foreign key constraint? The mock would pass, but the actual method would fail really?

I am just starting to think, that unless a method actually calculates some sort of logic, i dont see the point/reason for creating a unit test, especially when using Entity framework?

Aucun commentaire:

Enregistrer un commentaire