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

JustMock QueryableCollectionView

7 Answers 87 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Stephen
Top achievements
Rank 2
Stephen asked on 09 Jun 2011, 07:56 AM
Hi All,

I was just wondering if someone could post some example code of how to Mock a QueryableCollectionView?

I have a unit test that creates a class (injects a couple of Mocked services into constructor) and in the constructor of the viewmodel (that i'm testing) it sets up a collection view. The QueryableCollectionView takes property on the mocked dataservice as input and throws an exception. Not sure what I'm doing wrong.

var dataService = Mock.Create<IHoursEntryDataService>();
Mock.Arrange(() => dataService.PayrunComments).Returns(new EntitySet<PayrunCommentDto>());

is how I do the mock, and then inside the constructor of the viewmodel is this;

_commentsCollectionView = new QueryableCollectionView(_hoursEntryDataService.PayrunComments);

Which throws null exception error when i run the unit test.
PayrunComments is not null so not sure whats up with that.

help appreciated,
thanks,
Stephen

7 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 09 Jun 2011, 07:58 AM
Hello Stephen,

 Can you post the stack trace? 

Regards,
Vlad
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
Stephen
Top achievements
Rank 2
answered on 09 Jun 2011, 09:24 AM
at System.ServiceModel.DomainServices.Client.EntitySet.GetEnumerator()
 at System.ServiceModel.DomainServices.Client.EntitySet.System.Collections.IEnumerable.GetEnumerator()
 at Telerik.Windows.Data.EnumerableExtensions.TryGetFirstElement(IEnumerable source, Object& firstElement)
 at Telerik.Windows.Data.EnumerableExtensions.TryGetFirstElementType(IEnumerable source, Type& firstElementType)
 at Telerik.Windows.Data.EnumerableExtensions.CastToFirstElementType(IEnumerable source)
 at Telerik.Windows.Data.QueryableFactory.CreateQueryable(IEnumerable source)
 at Telerik.Windows.Data.QueryableCollectionView.InitializeQueryableSourceCollection()
 at Telerik.Windows.Data.QueryableCollectionView..ctor(IEnumerable sourceCollection)
 at PayrollSiteDataEntry.Client.ViewModels.HoursEntryViewModel.SetupCommentsCollectionView()
 at PayrollSiteDataEntry.Client.ViewModels.HoursEntryViewModel..ctor(ISession session, IHoursEntryDataService hoursEntryDataService, INavigationService navigationService, IPopupActivator popupActivator)
 at Client.UnitTests.HoursEntryViewModelTests.HoursEntryViewModel_Construction_IsNotNull()

Its a silverlight unit test. It gives me the above stack trace.

cheers,
Stephen
0
Vlad
Telerik team
answered on 09 Jun 2011, 09:32 AM
Hi,

 According to the stack trace the exception is raised from WCF RIA services class - you may need to mock your service as well. 

Best wishes,
Vlad
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
Stephen
Top achievements
Rank 2
answered on 09 Jun 2011, 09:37 AM
// Arrange
           var dataService = Mock.Create<IHoursEntryDataService>();
           var session = Mock.Create<ISession>();
           var popupActivator = Mock.Create<IPopupActivator>();
           var navigationService = Mock.Create<INavigationService>();
           Mock.Arrange(() => dataService.PayrunComments).Returns(new EntitySet<PayrunCommentDto>());
           // Act
           var viewModel = new HoursEntryViewModel(session, dataService, navigationService, popupActivator);
           Assert.IsNotNull(viewModel);

Thats my unit test. As you can see I've got a Dataservice (IHoursEntryDataService) which I'm mocking.
Is there perhaps something else on the dataservice that I need to Arrange?
I'm new at mocking and finding examples of mocking RIA Services has been difficult to find.

cheers,
Stephen
0
Vlad
Telerik team
answered on 09 Jun 2011, 09:43 AM
Hi Stephen,

 You need to mock GetEnumerator() method as well. 

Best wishes,
Vlad
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
Stephen
Top achievements
Rank 2
answered on 09 Jun 2011, 10:46 AM
Ah, I'm struggling with how to mock the GetEnumerator() method.
Would you be able to post a small example?

many thanks!
0
Ricky
Telerik team
answered on 15 Jun 2011, 08:50 AM
Hi Stephen,

In case you are mocking a collection or IQueryable implementation, you can use the ReturnsCollection from Telerik.JustMock.Helpers. Please take a look into the following example:
http://www.telerik.com/help/justmock/advanced-usage-mocking-linq-queries.html

Hope that solves your issue.

Kind Regards,
Ricky
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
Tags
GridView
Asked by
Stephen
Top achievements
Rank 2
Answers by
Vlad
Telerik team
Stephen
Top achievements
Rank 2
Ricky
Telerik team
Share this question
or