0

I need to add unit tests and increase code coverage of an application that is based o store procedure calls using Entity Framework.

Example 1:

    public virtual DbSet<Person> Persons { get; set; }

    public virtual IEnumerable<Person> PersonsFromSP()
    {
        return Persons.SqlQuery("spGetPersons").ToList();
    }

Example 2:

    public virtual IEnumerable<Animal> AnimalsFromSP()
    {
        return Database.SqlQuery<Animal>("spGetAnimals").ToList();
    }

There's also code with business logic and other stuff, but it's all based on these calls.

Whats a good approach at testing this?

Thanks in advance.

Emiliano Rodriguez
  • 454
  • 2
  • 6
  • 19

1 Answers1

1

I tried describing our approach in another answer: here.

Basically, you need to have an interface that your Context class will implement. The methods of the Context class will not be tested during unit testing because of the coupling to the database. However, you can test other classes (the business logic) that will reference the interface and not the concrete implementation.

The linked answer contains one detail of implementing the interface. We had two properties, only one of which was defined in the interface (AccountContacts in the example). The AccountContacts property will then relay the calls to the concrete implementation.

In your case, your Persons property will be the property that the clients of the Context will use. Then you can have another property, DbPersons that will actually be filled by the stored procedure.

Developer
  • 435
  • 4
  • 16