2

I'm using Entity Framework for my DAL. I'm trying to create unit test using rhino mock to test my repository. I'm stuck with trying to set return value for a property whose type is IQueryable<Restaurant>.

When I run my unit test, I keep getting

Unit.Tests.Infrastructure.Repository.WhenInvoked.ShouldReturnAllRestaurants: System.InvalidOperationException : Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw.

My code:

public interface IDbContext
{
    IQueryable<Restaurant> Restaurants { get; }
    IQueryable<Review> Reviews { get; }
    int SaveChanges();
    T Attach<T>(T entity) where T : class;
    T Add<T>(T entity) where T : class;
    T Delete<T>(T entity) where T : class; 
}

public class OdeToFoodDB : DbContext, IDbContext
{
    public DbSet<Restaurant> Restaurants { get; set; }
    public DbSet<Review> Reviews { get; set; }

    IQueryable<Restaurant> IDbContext.Restaurants
    {
        get { return Restaurants; }
    }

    IQueryable<Review> IDbContext.Reviews
    {
        get { return Reviews; }
    }

    int IDbContext.SaveChanges()
    {
        return SaveChanges(); 
    }

    T IDbContext.Add<T>(T entity)
    {
        return Set<T>().Add(entity); 
    }

    T IDbContext.Delete<T>(T entity)
    {
        return Set<T>().Remove(entity);
    }

    T IDbContext.Attach<T>(T entity)
    {
        var entry = Entry(entity);
        entry.State = System.Data.EntityState.Modified;
        return entity;
    }
}

public class RestaurantRepository : IRestaurantRepository
{
    private IDbContext _db;

    public RestaurantRepository()
    {
        _db = ObjectFactory.GetInstance<IDbContext>();
    }

    public RestaurantRepository(IDbContext db)
    {
        _db = db;
    }


    public List<Restaurant> GetAllRestaurants()
    {
        return _db.Restaurants.ToList(); 
    }
}

    [Test]
    public void ShouldReturnAllRestaurants()
    {
        _db.Stub(m => m.Restaurants.AsQueryable()).Return(_restaurant.AsQueryable());

        var sut = _restaurantRepository.GetAllRestaurants();
        //Some assert statement later. 
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • What to you gain by unit testing this? I would rather integration-test this which is simpler and more benifitial – Marius Dec 30 '12 at 08:44
  • I'm a new to TDD. I thought that every piece of code, there should be a unit test for it, if you are using TDD approach. So I thought to write unit test for the repository first. Then have another integration test that actually doesn't use mock to test actual data is obtain from the database by setting up and tearing down the db. – Jeremy Yaskik Dec 31 '12 at 18:28
  • 1
    As a sidenote. You should not be testing against IQueryables at all. The differences between the IQueryable you test against, and the IQueryable you use in production, is probably huge. Your production IQueryable uses a queryprovider to query a database. There are limitations to what operations and types you can use when you query a database. See http://stackoverflow.com/questions/9354716/linq-to-ef-unsupported-functions for starters. However, your tests are running against a in-memory list of items, and another queryprovider with more supported functions, will be used. – Mattias Nordqvist Apr 18 '13 at 20:54

2 Answers2

0

You can just create an array/collection etc. and use .AsQueryable()

MarkG
  • 1,859
  • 1
  • 19
  • 21
0

I figure out the answer. In Rhino, property Restaurants doesn't have to be .AsQuerable() when you are trying to specified the property in the Stub() method.

    [Test]
    public void ShouldReturnAllRestaurants()
    {
        _db.Stub(m => m.Restaurants).Return(_restaurant.AsQueryable());

        var sut = _restaurantRepository.GetAllRestaurants();
        //Some assert statement later. 
    }