chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
ProductsDataContext productsDataContext = Isolate.Fake.Instance<ProductsDataContext>();
Isolate.WhenCalled(() => productsDataContext.CAD_CatalogProducts).WillReturn(GetCanadianProducts());

I have a products data context that has 4 tables. Each table is a product catalog. I need to fake a return value from one of the tables.

The first two lines are the way I've started to attempt this. I'm open to any method that will get the job done.

Having searched this forum I see a way to do this with the MockManager and recorder. I'm looking for a 6.0+ means of doing this with the AAA API.

-many thanks
asked by HoltMansfield (840 points)

8 Answers

0 votes
Hi,

The best way is to use AAA API, the direction you took is the right one.

It is also possible to do it using previous generation of the API (natural mocks) but the new one is much more readable and maintainable.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Hi Elisha,

Thanks for getting back to me. I should have been more clear in my original post. The code I posted is not working. The function 'GetCanadianProducts()' needs to return a System.Data.Linq.Table and that can't really be instantiated.

Can you post any examples at all for me to look at?
answered by HoltMansfield (840 points)
0 votes
Bump.

If anyone has anything for me to look at. I'd greatly appreciate it.
answered by HoltMansfield (840 points)
0 votes
Hi,

I'll start with example on to fake table:

var fakeDataContext = Isolate.Fake.Instance<MyDataContext>();

var fakeCategoriesTable = Isolate.Fake.Instance<Table<Category>>();
Isolate.WhenCalled(() => fakeDataContext.Categories).WillReturn(fakeCategoriesTable);

var fakeCategory = Isolate.Fake.Instance<Category>();
Isolate.WhenCalled(() => fakeCategoriesTable)
    .WillReturnCollectionValuesOf(new[] {fakeCategory});

CollectionAssert.AreEqual(new[] {fakeCategory}, fakeDataContext.Categories.ToList());


In this example we've faked a Table<T> using Isolate.Fake.Instance<Table<Category>>(), it allows us to create instances of types with no public constructor.

Using Isolate.WhenCalled(() => fakeDataContext.Categories).WillReturn(fakeCategoriesTable); we were able to return this table from the Categories property of the data context.

I'll add some more info on this thread about unit testing LINQ soon.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Thank you so much Elisha. This is definitely a starting point for me.

Here is the method I'm trying to test.


List<PriceAuditProduct> priceAuditProducts = new List<PriceAuditProduct>();

try
{
using (ProductsDataContext db = new ProductsDataContext(connectionString))
{

foreach (var product in db.MasterCatalog_CatalogProducts)
{

if (!String.IsNullOrEmpty(product.ProductID))
{

PriceAuditProduct priceAuditProduct = new PriceAuditProduct();
priceAuditProduct.ProductId = product.ProductID;
priceAuditProduct.VariantId = product.VariantID;
priceAuditProduct.PriceLockedAcrossCurrencies = Convert.ToBoolean(product.PriceLockedAcrossCurrencies);
priceAuditProduct.CAD_Price = db.CAD_CatalogProducts.Single(p => p.ProductID == product.ProductID && p.VariantID == product.VariantID)._listprice_inherited;
priceAuditProduct.USD_Price = db.USD_CatalogProducts.Single(p => p.ProductID == product.ProductID && p.VariantID == product.VariantID)._listprice_inherited;
priceAuditProduct.EUR_Price = db.EUR_CatalogProducts.Single(p => p.ProductID == product.ProductID && p.VariantID == product.VariantID)._listprice_inherited;
priceAuditProduct.GBP_Price = db.GBP_CatalogProducts.Single(p => p.ProductID == product.ProductID && p.VariantID == product.VariantID)._listprice_inherited;

priceAuditProducts.Add(priceAuditProduct);
}
}

return priceAuditProducts;
}
}
catch (Exception ex)
{
return null;
}
answered by HoltMansfield (840 points)
0 votes
Hi Holt,

You can use the method WillReturnCollectionValuesOf() to have Isolator return a list of values as your test data:

Isolate.WhenCalled(() => fakeDataContext.CAD_CatalogProducts)
                .WillReturnCollectionValuesOf(GetFakeCanadianProducts()); 


However, in order to query this data via LINQ, it must be converted to IQueryable<T>. You can use the .ToQueryable() extension method to do this.

The following test shows how to fill a Table of Products with fake test data:

[Test, Isolated]
public void FakeDataContext_ReturnsFakeProducts()
{
    var fakeDataContext = Isolate.Fake.Instance<ProductsDataContext>();
    
    Isolate.WhenCalled(() => fakeDataContext.CAD_CatalogProducts)
        .WillReturnCollectionValuesOf(GetFakeCanadianProducts());

    var products = from p in fakeDataContext.CAD_CatalogProducts
                   where p.ListPrice > 200 && p.ListPrice < 1000
                   select p;
    
    Assert.AreEqual(1, products.Count());
}

private IQueryable<CAD_CatalogProduct> GetFakeCanadianProducts()
{
    return new List<CAD_CatalogProduct>
    {
        new CAD_CatalogProduct { ProductID = 1, VariantID = 10, ListPrice = 500 },
        new CAD_CatalogProduct { ProductID = 2, VariantID = 20, ListPrice = 1000 }
    }.AsQueryable();
}


Please let me know if this helps you out, or if there's anything else I could help you with.

Regards,
Igal,
Typemock Support
answered by igal (5.7k points)
0 votes
hmemcpy: Thank you kindly. This is fabulous and it gave me total control.

I fake out the Master Catalog that I iterate on, and fake out the other tables that get accessed. At the end I'm using a Method that our team lead wrote that compares just about anything. We use it extensively in our Unit Tests. I compare what the method returns to a canned list of identical values.

-thanks hmemcpy & Elisha...


[TestMethod, Isolated]
public void GetProducts_Positive()
{
PriceAuditProductRepository priceAuditProductRepository = new PriceAuditProductRepository();
ProductsDataContext productsDataContext = Isolate.Fake.Instance<ProductsDataContext>();

Isolate.WhenCalled(() => productsDataContext.MasterCatalog_CatalogProducts).WillReturnCollectionValuesOf(GetFakeMasterProducts());
Isolate.WhenCalled(() => productsDataContext.CAD_CatalogProducts).WillReturnCollectionValuesOf(GetFakeCanadianProducts());
Isolate.WhenCalled(() => productsDataContext.USD_CatalogProducts).WillReturnCollectionValuesOf(GetFakeAmericanProducts());
Isolate.WhenCalled(() => productsDataContext.GBP_CatalogProducts).WillReturnCollectionValuesOf(GetFakeUK_Products());
Isolate.WhenCalled(() => productsDataContext.EUR_CatalogProducts).WillReturnCollectionValuesOf(GetFakeEuroProducts());

Isolate.Swap.NextInstance<ProductsDataContext>().With(productsDataContext);

List<PriceAuditProduct> products = (List<PriceAuditProduct>)Isolate.Invoke.Method(priceAuditProductRepository, "GetProducts", ConfigurationManager.AppSettings["DB_Server_Name"]);

Assert.IsTrue(products.ValEquals<PriceAuditProduct>(GetExpectedProducts()));
}
answered by HoltMansfield (840 points)
0 votes
Thanks for this post,really helps on me....
how to treat depression
God bless!!!
answered by Jiellen29 (140 points)
...