Telerik OpenAccess is an Enterprise-grade .Net ORM that does the data access plumbing in desktop and web applications. Supporting forward (model-first) and reverse (schema-first) mapping, the tool offers transparent persistence for your data access layer and business objects. Telerik OpenAccess provides tight Visual Studio integration and allows you to seamlessly create database independent code.

Telerik JustMock can be used in conjunction with Telerik OpenAccess to fake the data access layer to make it easier for you to test your target logic without worrying about its dependencies.

In this topic we will cover some scenarios in unit testing Telerik OpenAccess.


In the examples below LINQ queries are used. To run the tests you first need to go to elevated mode by enabling Telerik JustMock from the menu. Refer to How to Enable/Disable Telerik JustMock? For more information about mocking LINQ queries go to Mocking LINQ Queries topic.

Mocking LINQ Queries In Telerik OpenAccess

In our first example we will mock LINQ queries. Prior to writing the test, two methods are defined as follows:

C# Copy imageCopy
public Category GetCategoryByName(IObjectScope scope, string name)
    return (from cat in scope.Extent<Category>()
        where cat.CategoryName == name
        select cat).SingleOrDefault();

public IEnumerable<Category> GetCategories()
    IList<Category> categories = new List<Category>();

    categories.Add(new Category { CategoryID = 1, CategoryName = "Food", Description = "Continental" });
    categories.Add(new Category { CategoryID = 2, CategoryName = "Beverage", Description = "Cold" });

    return categories;

The GetCategories method returns a fake collection of Category objects. By the GetCategoryByName method we retrieve a specific element of the collection that matches the name argument we have passed.

We will run the LINQ query into the fake collection to assert the expected behavior of this method. This is exactly what we do in the next code sample. This is the actual test. We arrange the IObjectScope.Extent method to return the fake collection and then execute the GetCategoryByName method with argument "Beverage". This will result in the second element in the fake collection and thus its Description property is "Cold". Therefore, the assertion at the end of the test passes.

C# Copy imageCopy
// Arrange
var scope = Mock.Create<IObjectScope>();
Mock.Arrange(() => scope.Extent<Category>()).ReturnsCollection(GetCategories());

// Act
var cat = GetCategoryByName(scope, "Beverage");

// Assert
Assert.AreEqual("Cold", cat.Description);

Mocking Transactions

The next example is a little bit more complicated. Here we mock several aspects of the IObjectScope object.

C# Copy imageCopy
// Arrange
Product newProduct = new Product();
newProduct.ProductID = 3;
newProduct.ProductName = "TestProduct";

IList<Product> products = new List<Product>();
bool called = false;

var scope =  Mock.Create<IObjectScope>();
var transaction = Mock.Create<ITransaction>();

Mock.Arrange(() => transaction.Begin()).DoInstead(() => { called = true; });
Mock.Arrange(() => transaction.Commit()).DoInstead(() => products.Add(newProduct));

Mock.Arrange(() => scope.Transaction).Returns(transaction);
Mock.Arrange(() => scope.Extent<Product>()).ReturnsCollection(products);

// Act

IQueryable<Product> productQuery = scope.Extent<Product>().Where(p => p.ProductID == 3);

// Assert
Assert.AreEqual(1, productQuery.Count());

This is what we do here:

  1. First, we create a fake Product. Its ProductID property is set to 3 and ProductName to "TestProduct".
  2. Create a fake instance of IObjectScope.
  3. Create a fake instance of ITransaction.
  4. Mock the fake instance from previous step so that:
    • when Begin() is called a local boolean variable is set to true
    • when Commit() is called the fake Product from step 1. is inserted in local collection of Product
  5. Mock the fake instance from step 2. so that:
    • Transaction property get results in our fake object from step 3.
    • when Extent() is called the local fake collection is returned
  6. Now, acting:
    • scope.Transaction.Begin() - the local boolean is set to true
    • scope.Transaction.Commit() - the fake Product is added to the collection
    • scope.Extent<Product>().Where(p => p.ProductID == 3) - the LINQ query will be executed on the fake collection and the only element will be returned
  7. Finally, assertion.

See Also