This is a migrated thread and some comments may be shown as answers.

[Solved] Mocking IObjectScope.Extent<T> collection

5 Answers 185 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Denis Vulinovich
Top achievements
Rank 1
Denis Vulinovich asked on 11 Mar 2010, 07:06 AM
I've just started using the Moq framework to speed up my unit tests and reduce their dependence on the database. For example, I can use a mock IObjectScope to easily verify the a Repository method calls the IObjectScope.Add() method as follows:

// Create mock IObjectScope 
Mock<IObjectScope> _mockScope = new Mock<IObjectScope>(); 
 
// Call the repository method... 
 
// Verify that IObjectScope.Add() was called 
_mockScope.Verify( mS => mS.Add( myObject ) ); 
 

But when I mock IObjectScope.Extent<T>(), I want to setup the method to return a collection of objects that I've created, as follows:

_mockScope.Setup( mS => mS.Extent<MyClass>() ).Returns( /* Collection of objects */ ); 
 

I don't know how to create a collection of objects to match the return type of ExtentQuery<T>. The class has no constructor, and I haven't been able to cast an IEnumerable<T> collection to that type.

So any help would be appreciated.

Thanks,
Denis

5 Answers, 1 is accepted

Sort by
0
Sheldon Hall
Top achievements
Rank 1
answered on 15 Mar 2010, 12:57 PM
Hi,

I've been trying to do this myself, did you come up with a solution as yet?

If so, can you please share it?

Thank You
0
Sergej Mertens
Top achievements
Rank 1
answered on 15 Mar 2010, 04:10 PM
Hi Denis and Sheldon,

what is the test case and what is the system under test. I currently don't understand _what_ you want to test. For me it doesn't make sense to test this. If I understand what you want to do, then maybe I can help you.

Greetings,
Daniel


0
Sheldon Hall
Top achievements
Rank 1
answered on 15 Mar 2010, 06:57 PM
Hi,

I have a simple test case. I just want to get all the records in a table using the ORM

I want to mock the IObjectScope.Extent method so that I can return a defined list of objects to test the logic of my code?

I know the Add method and transaction stuff can me mocked but it doesn't seem that the Extent method can be.

Do you have any thoughts on this?
0
Jordan
Telerik team
answered on 16 Mar 2010, 04:11 PM
Hello Sheldon Hall,

Unfortunately there is currently no easy way to mock the Extent method.
However, we are aware that testability (and mock-ability)  are important and will be working to make this easier in future releases.
And for now, if I wanted to be able to mock my data source I would use some kind of a repository pattern and implement a repository class that is a thin wrapper around the object scope and mock that.
Of course this means that all my code will have to use this repository class (or some interface that it implements) and not the object scope directly.

Greetings,
Jordan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Denis Parchenko
Top achievements
Rank 1
answered on 15 Nov 2010, 10:07 AM
As long as you don't need ExtentQuery<T> specifics you may use this:

public class ExtentQueryMock<T> : EnumerableQuery<T>, ExtentQuery<T>
{
    public ExtentQueryMock(IEnumerable<T> source)
        : base(source) { }
 
    public ExtentQuery<T> Debug(bool show)
    {
        throw new NotImplementedException();
    }
 
    public ExtentQuery<T> ForwardsOnly(bool noRandomAccess)
    {
        throw new NotImplementedException();
    }
 
    public ExtentQuery<T> IgnoreUncommitted(bool doNotFlush)
    {
        throw new NotImplementedException();
    }
 
    public ExtentQuery<T> ParallelFetch(bool allowParallelFetch)
    {
        throw new NotImplementedException();
    }
}
Tags
General Discussions
Asked by
Denis Vulinovich
Top achievements
Rank 1
Answers by
Sheldon Hall
Top achievements
Rank 1
Sergej Mertens
Top achievements
Rank 1
Jordan
Telerik team
Denis Parchenko
Top achievements
Rank 1
Share this question
or