1

I'm writing a Test Cases for a Common generic methods, In that I don't have any database configuration, but the method generic method is handling the IQueryable object. So, I tried the following code

public static DbSet<Person> GetPersonDbSet() {
    DbSet<Person> record = null;
    record.Add(new Person() {
        PersonId = 1,
        FirstName = "Ram",
        LastName = "Kumar"
    });

    record.Add(new Person() {
        PersonId = 2,
        FirstName = "Raj",
        LastName = "Kumar"
    });

    return record;
}

Original Method:

public static class MyLib {
    public static IQueryable Pagination<T>(IOrderedQueryable<T> query, int offset, int limit) {

        // Validation Code (...)

        // Main Logic
        return query.Skip(offset).Take(limit);

    }
}

Test Method:

[Fact]
public void CheckPagination() {
    var query = GetPersonDbSet().OrderBy(i => i.FirstName);

    var result = MyLib.Pagination(query, 1, 5);
    int count = result.Count();
    Assert.True(count > 0);
}

I got an exception in GetPersonDbSet() while on adding the entity, because the variable DbSet<Person> record was initialized with null. Kindly assist me how to Initialize and achieve this without a context (because in this project I'm not using any Database Context).

1 Answers1

2

I'd start with:

DbSet<Person> record = new TestDbSet<Person>();

and have a read of https://msdn.microsoft.com/en-us/library/dn314431(v=vs.113).aspx (which includes the definition of TestDbSet).

mjwills
  • 23,389
  • 6
  • 40
  • 63