0

I have spent some time trying to understand the topic of unit testing as it relates to using EF 5.0/DbContext. The function below is the one to unit test. From my basic understanding it seems I have the following options.

  1. I create a repository interface. One is the real implementation and the other is the fake one. The fake one would use data I would create from the entity objects. In this case I could create a List... but the problem seems to be that linq->objects (the test) is not the same as linq->EF so im not really testing anything ?

  2. Create a small testing DB somehow using the information from the DBContext.. and then add the data to the DB and retrieve it.. though not sure how I would do that. how do I get a DB created from my Dbcontext as part of a MSunit test run ?

  3. Do not need to unit test. It is really just testing the 'Where' extension method of the EF context?

So.. can someone explain the proper way to think about this without getting too advanced? I understand the basic concepts of unit testing repository pattern when you are calling them from MVC controllers which are most demos.

   public class MSAManager// : IMSAManager
{
    private MATT_LocalStatisticsEntities context = new MATT_LocalStatisticsEntities();

    //Takes series of FIPS codes and sees if they exist in the DB
    public bool IsPlaceExistsV1(string FIPS_SMA, string FIPS_StateCode, string FIPS_CountyCode, string FIPS_EntityCode, DateTime year)
    {
        var duplicate = context.Places.Where(x => ((x.FIPSMSA == FIPS_SMA) &&
                                                   (x.FIPSState == FIPS_StateCode) &&
                                                   (x.FIPSCounty == FIPS_CountyCode) &&
                                                   (x.FIPSEntity == FIPS_EntityCode) &&
                                                   (x.StartDate == year)
                                                  ));

        return (duplicate.Count() != 0);
    }
punkouter
  • 5,170
  • 15
  • 71
  • 116
  • 1
    If you want to effectively be able to unit test that, you are going to need a mocking tool. In order to use most mocking tools you are going to need to use objects that implement interfaces, as most mocking tools depend on mocking the interface. I would recommend using a dependency injection approach, and using a Unit of Work and Repository pattern for your data access. Then you can mock the unit of work. Couple this with IOC and you have a good base to start with. – Maess May 23 '13 at 17:41
  • In this case what would I mock? If I mock the expected result then there is nothing left to test... Or maybe I do not understand mocking.. If there is a simple code sample that uses all these concepts I could absorb that would be great. – punkouter May 23 '13 at 17:44
  • You would mock the repository and load the mock with data to test – Maess May 23 '13 at 17:45

2 Answers2

1

Regarding 3 (see my comment regarding the rest):

Yes you need to unit test as what you are testing is that a duplicate is a Place that has the same FIPSMSA, FIPSState, FIPSCounty and StartDate as another place.

Edit OP asked for an example of a mock:

This assumes you use MOC and that you have a unit of work that you are mocking, which exposes a repository called SomeEntityRepository

Moq.Mock<IUnitOfWork> _mockUow;
_mockUow = new Moq.Mock<IUnitOfWork>();
var entity = new SomeEntity();
_mockUow.Setup(m => m.SomeEntityRepository.GetById(Moq.It.IsAny<int>())).Returns(entity);

You would then inject the mock unit of work into the constructor of the object which has the method you wish to test or into the method you wish to test, assuming either the constructor or the method take a paramenter who's type is IUnitOfWork

Maess
  • 4,118
  • 20
  • 29
1

You can also perform integration tests instead of direct unit tests.

You can have integration tests against a real EF DBContext, just give it a different connection string in the App.config/Web.Config of the unit-tests project.

Read this and all of the answers to it.

You can see an implementation example here

Community
  • 1
  • 1
Liel
  • 2,407
  • 4
  • 20
  • 39
  • those 4 lines (from the link)is all I need to create a new database from my Dbcontext?? And then I can go ahead and insert test data.. so then to I have to pass in my testContext some how to my function. right ? – punkouter May 23 '13 at 18:31
  • Correct, just type a different connection string in the App.Config, so the DbContext will point to a Test Database. – Liel May 23 '13 at 18:59
  • ok.. that seems to me the only true way to test functions on a DBContext – punkouter May 23 '13 at 19:26
  • So then this set of tests would be in a different category than your other unit test right ? it is an 'integration' test yes ? – punkouter May 23 '13 at 19:27