-2

How do I stub these methods:

ProductDAL:

    public IQueryable<string> getCatNameList()
    {
        var db = new Database();
        var categoryNames = db.ProductCategories.Select(c => c.Name);
        return categoryNames;
    }

    public IQueryable<int> getCatIdList()
    {
        var db = new Database();
        var categoryID = db.ProductCategories.Select(c => c.Categoryid);
        return categoryID;
    }

IProductDAL:

    IQueryable<int> getCatIdList();
    IQueryable<string> getCatNameList();

Been searching on stackoverflow, but no questions/answers that gets me any closer to understanding how I go about doing this.

aurelius
  • 3,946
  • 7
  • 40
  • 73
kuj asl
  • 23
  • 4
  • 1
    What exactly do you want to create a stub for? The database? – Yacoub Massad Nov 07 '15 at 00:43
  • You need to provide more information, such as when you want to use the stubbed methods. Also, you should do a Google search for a tutorial on unit testing with C#. – Derek Van Cuyk Nov 07 '15 at 00:46
  • @DerekVanCuyk I changed the IQueryable's to IEnumerable. I'm going to use the stubbed methods in unit testing, but I just don't seem to understand what way I should go about this. Fairly new to all this so yea. – kuj asl Nov 07 '15 at 01:02
  • In this paradigm you would likely have a MockProductDAL for unit testing and have a few hardcoded Categories in a list, and then just go _myCategories.Select(c => c.CategoryId) for your getCatIdList method. Just having a stub method with no real values may not be very useful for unit testing. – James Nov 07 '15 at 01:26

2 Answers2

2

In this case I would recommend you change your interface to a series of IEnumerable rather than IQueryable. There is little point having these methods as queryables as they have already been projected.

If you are just trying to stub them, use Enumerable.Empty<type>().AsQueryable().

James
  • 489
  • 1
  • 5
  • 15
  • I changed them to IEnumerable, but how do Istub the categoryNames and categoryID? example would be lovely. – kuj asl Nov 07 '15 at 01:28
  • public IEnumerable getCatIdList() { return Enumerable.Empty(); } would be potential stub if you just want the code to compile – James Nov 07 '15 at 02:01
1

Create and IEnumerable of whatever you need to return (can be a List or an array). Then convert it to IQueryable using AsQueryable extension method and pass it to whatever mocking/stubbing library you use.

Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39